如何使用Moment.js获取今天EST的开始时间和当前时间

Pra*_*abu 1 javascript timezone node.js momentjs

我想使用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"));
Run Code Online (Sandbox Code Playgroud)

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

这是对还是错?

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

Sha*_*oor 5

首先,当你使用momentjs时,使用明确的STOP 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);
Run Code Online (Sandbox Code Playgroud)