goredwards
goredwards

Reputation: 2494

Date.now() not working in angularjs 1.3.5

i'm trying to get a local timestamp in a controller that won't be used in a view (ie filter not an option)
(for info it's to be written into an SQLite db as a local 'date created' field)

the following gets me the date - but is in the wrong format:

var current_time = new Date();
console.log('current_time:', current_time);

which gives me date in the format current_time: Thu Dec 18 2014 23:16:27 GMT-0800 (PST)

the following also works (which partially solves my problem - since i think i can use UTC):

var current_time = new Date().getTime();
console.log('current_time:', current_time);

which gives current_time: 1418973515745 however it's UTC according to the docs

however this (which i thought was the solution) does not work:

var current_time = new Date.now();
console.log('current_time:', current_time);

gives me the following error:
TypeError: function now() { [native code] } is not a constructor

according to the javascript docs I've got the right formats etc...
the Angular docs don't mention anything specific (mostly seems to be how to get from timestamp to human readable, not the other way around)

so for curiosity:

Upvotes: 0

Views: 1476

Answers (2)

goredwards
goredwards

Reputation: 2494

rookie error:

it was the "new" that was causing the problem: this works fine

var current_time = Date.now();
console.log('current_time:', current_time);

output = current_time: 1418974955726

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1074008

You don't use new with Date.now(), it's just:

var current_time = Date.now();

As the error message is telling you, Date.now isn't a constructor function.

Upvotes: 3

Related Questions