George Reason
George Reason

Reputation: 173

Momentjs get current GMT unix time

Backend API for a project I am working on only accepts the UNIX time.

The server is set in GMT so to send the UNIX time in GMT I am having to manipulate the data according to the timezone to get the results I need.

Is there a better way than below in momentJS to get the current UNIX time in GMT?

  var timeOffset = moment().utcOffset();

  if (timeOffset >= 0) {
    this.sinceNow = moment().subtract(timeOffset, 'minutes').unix();
  }
  else {
    this.sinceNow = moment().add(timeOffset * -1, 'minutes').unix();
  }

Upvotes: 1

Views: 17293

Answers (2)

Ashish Gupta
Ashish Gupta

Reputation: 1241

Please us to get GMT TimeStamp using moment.js

var moment = require('moment');
dt = moment();
dt.subtract(dt.parseZone().utcOffset(), 'minutes')

console.log(dt.unix());

Upvotes: 0

Thomas Weglinski
Thomas Weglinski

Reputation: 1094

According to MomentJS documentation I believe it will be:

var gmt = moment().utc(), // this gives the UTC/GMT time
    unix = gmt.unix();    // this gives the Unix time

Upvotes: 3

Related Questions