Reputation:
How can I get current date and time in angularjs
I have tried the following:
<b>{{Date.Now}}</b>
But it doesn't work. Any suggestions would be appreciated.
Upvotes: 14
Views: 113275
Reputation: 1513
In Component file:
this.registerDate = new Date().toJSON("yyyy/MM/dd HH:mm");
this.updateDate = new Date().toJSON("yyyy/MM/dd HH:mm");
In Template file:
<span class="font-size-13px">{{registerDate}}</span>
<span class="font-size-13px">{{updateDate}}</span>
Upvotes: 3
Reputation: 51
In your controller
scope.v.Dt = Date.now();
The DOM syntax is
{{v.Dt | date:'medium'}}
For an example -
Output -
Oct 28, 2010 8:40:23 PM
Upvotes: 5
Reputation:
Thanks all I got it by your all of answer
<b>{{getDatetime | date:'yyyy-MMM-dd'}}</b>
In controller,
$scope.getDatetime = new Date();
Upvotes: 21
Reputation: 12508
In your controller - scope.v.Dt = Date.now();
The DOM syntax is -
{{v.Dt | date:'medium'}}
For an example -
Output -
Oct 28, 2010 8:40:23 PM
For all possible date formats - http://docs.angularjs.org/api/ng.filter:date
Upvotes: 3
Reputation: 43526
Define getDatetime method on scope
scope.getDatetime = function() {
return (new Date).toLocaleFormat("%A, %B %e, %Y");
};
Then in template:
<b>{{getDatetime()}}</b>
Doc to format date https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleFormat
Upvotes: 15