【问题标题】:How to get today start time and current time of EST using Moment.js如何使用 Moment.js 获取 EST 的今天开始时间和当前时间
【发布时间】:2016-02-12 10:44:58
【问题描述】:

我想使用 Moment.js (EST) 创建开始和结束时间戳:

  • StartTime 将是今天的开始时间
  • EndTime 将是当前时间。

我使用了moment.js并像这样创建

var time = new Date();
var startTime=Date.parse(moment(time).startOf('day').tz('America/New_York').format("MM/DD/YYYY HH:mm:ss"));
var endTime=Date.parse(moment(time).tz('America/New_York').format("MM/DD/YYYY HH:mm:ss"));

它以毫秒为单位给出时间。

是对还是错?

我没有从 db 获取数据,因为时间戳不匹配。

【问题讨论】:

  • 为什么不使用 Date 对象?我认为你不需要时间。你想做什么??
  • 如果我使用 Date 对象,它将给出当前时区时间,但我只需要 EST 时区,所以我使用 moment.js
  • OK.. 这不是不使用日期的原因..

标签: javascript node.js timezone momentjs


【解决方案1】:

首先,当您使用 momentjs 时,请停止使用 Date 明确:

var moment = require('moment-timezone');

// moment() without parameter means the current time
// toDate() converts the moment object to a javascript Date
var startTime = moment().tz('America/New_York').startOf('day').toDate();
var endTime = moment().tz('America/New_York').toDate();

// startTime and endTime are Date objects    
console.log(startTime);
console.log(endTime);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2015-03-23
    • 2014-10-18
    • 2022-01-18
    相关资源
    最近更新 更多