user2998788
user2998788

Reputation:

How to get Current Date time

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

Answers (5)

Hai Dinh
Hai Dinh

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>

Result will return: enter image description here

Upvotes: 3

Anshu
Anshu

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

user2998788
user2998788

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

Sujit Agarwal
Sujit Agarwal

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

Daiwei
Daiwei

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

Related Questions