Cmag
Cmag

Reputation: 15750

momentJS get UTC timestamp

Folks, I am unable to get the UTC timestamp using the momentjs. Hope someone can point me in the right direction

var start = Date.now();
var utc = moment.utc(start).toDate();

or

var utc = moment.utc().toDate();
Tue Nov 11 2014 13:45:13 GMT-0500 (EST)

Returns the EST timezone I am in, not UTC. How do i get the Javascript Date in UTC?

If I do

var utc= moment.utc();
console.log(utc);

output is

 { _useUTC: true,
  _isUTC: true,
  _l: undefined,
  _i: undefined,
  _f: undefined,
  _d: Tue Nov 11 2014 13:43:21 GMT-0500 (EST) }

Thanks

Upvotes: 29

Views: 53533

Answers (3)

Tyler Collier
Tyler Collier

Reputation: 11990

I wanted to know how to get the number of SECONDS, not milliseconds.

In moment.js 1.6.0, use this if you want the timestamp in seconds:

moment.utc().unix()

@Charles Foster's answer deals with milliseconds, which has been around since moment.js 1.0.0:

moment.utc().valueOf()

Upvotes: 1

Charles Foster
Charles Foster

Reputation: 539

Simplest answer from the comments :

moment.utc().valueOf()

Gives UTC timestamp

Upvotes: 34

Daniel
Daniel

Reputation: 38761

var moment = require('moment');

// timestamp with UTC time
console.log(moment.utc().format('ddd MMM DD YYYY HH:mm:ss z'));

// or via the date object
console.log(moment.utc().toDate().toUTCString());

Upvotes: 10

Related Questions