javascript - Momentjs get current GMT unix time - Stack Overflow

Backend API for a project I am working on only accepts the UNIX time. The server is set in GMT so to se

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

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();
  }
Share Improve this question edited Jun 4, 2015 at 20:02 George Reason asked Jun 4, 2015 at 19:54 George ReasonGeorge Reason 1731 gold badge3 silver badges13 bronze badges 1
  • As a friendly reminder to yourself and others that may read this: Any time you find yourself manually manipulating time zone offsets, in any language, you are probably not using the correct API. There are very few exceptions unless you are writing a date/time library. – Matt Johnson-Pint Commented Jun 5, 2015 at 17:02
Add a ment  | 

2 Answers 2

Reset to default 3

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744195719a4562647.html

相关推荐

  • javascript - Momentjs get current GMT unix time - Stack Overflow

    Backend API for a project I am working on only accepts the UNIX time. The server is set in GMT so to se

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信