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

5

3 Answers

Simplest answer from the comments :

moment.utc().valueOf()

Gives UTC timestamp

1
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());
10

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest