0

i have an input type Date in my HTML.(i'm working with angularjs )
i used it like this :

< input ng-model="voyage.dateRetour" type="date">   
{{voyage.dateRetour}}

i created a pdf with jsPDF and i use the inputs form the complete the pdf so i did :

     $scope.createPDFs= function (filename) { 
     var doc = new jsPDF();
  doc.text(13, 95, 'Retour : '+$scope.voyage.dateRetour ); 

and it gives me : Fri Aug 21 2015 00:00:00 GMT+0200 (Paris, Madrid (heure d’été))

i dont know how to change the format to short format like DD-MM-YYYY .

i tried to do a filter like :

$filter('date')($scope.voyage.dateRetour, "dd/MM/yyyy");

Thank you for helping.

5
  • try replacing {{voyage.dateRetour}} with {{voyage.dateRetour | date:"dd/MM/yyyy"}} Commented Aug 21, 2015 at 10:22
  • thank you for responding, i need the date in my controller for use it in pdf with jsPDF , so i need a filter in my controller not in HTML. Commented Aug 21, 2015 at 10:36
  • Similar question is already answered in previously stackoverflow.com/questions/20131553/… Commented Aug 21, 2015 at 10:48
  • create a plunkr where we can see the exact issue you're facing please. Commented Aug 21, 2015 at 11:04
  • i can't create plunker because i need the plugin of jsPDF but i can explain : i created a pdf with jsPDF and i use the inputs form the complete the pdf so i did $scope.createPDFs= function (filename) { var doc = new jsPDF(); doc.text(13, 95, 'Retour : ' + $scope.voyage.dateRetour ); and it gives me Fri Aug 21 2015 00:00:00 GMT+0200 (Paris, Madrid (heure d’été)) Commented Aug 21, 2015 at 12:08

4 Answers 4

1

Try this:

    <div ng-app ng-controller="MyCtrl">{{date | date:'dd MMM yyyy - hh:mm a'}}
    <br />{{date}}</div>

i.e using the filter in the html itself.

Demo: http://jsfiddle.net/Blackhole/srnug/

Sign up to request clarification or add additional context in comments.

1 Comment

sorry i think i didn't ask wwell my question , for the html i just tested it , i need the date in my controller for use it in pdf with jsPDF , so i need a filter in my controller not in HTML.
1

Checkout this it will help you:--

function MyController($scope){
  $scope.MyObje = {mydate:new Date()}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>

<div ng-app ng-controller="MyController">
  <input type="date" ng-model="MyObje.mydate">

  {{MyObje.mydate | date:'dd/MM/yyyy'}}
</div> 

Comments

0

First split for date and time. Second to replace all "-" to "/"

< input ng-model="voyage.dateRetour" type="date">   
{{voyage.dateRetour.split("T")[0].split("-").join("/");}}

2 Comments

Thanks you but it doesn't work for me like i said in a comment i need the date in my controller for use it in pdf with jsPDF , so i need a filter in my controller not in HTML.
okay, write the code in the bracket in your controller. it will work if date is given
0
/* code to format date into controller */

 app.controller('homeController', function($scope,$filter) {
    $scope.date = $filter("date")(new Date('2017-12-04'),'dd/MM/yyyy');
    alert($scope.date);
 } 

2 Comments

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
@VishalChhodwani this is just a sample u can modify as per your requirement. U can call it inside a function or use in html. In short you can modify this code as your need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.