How to get today start time and current time of EST using Moment.js

房东的猫 提交于 2019-12-24 13:52:34

问题


I want to create Start and End Time stamp using Moment.js (EST):

  • StartTime would be today's start time
  • EndTime would be current time.

I have used moment.js and created like this

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

It is giving time in milliseconds.

Is it correct or wrong?

I am not getting data from db because there is mismatch in Time stamp.


回答1:


First thing first, when you use momentjs, STOP using Date explictly:

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


来源:https://stackoverflow.com/questions/35360573/how-to-get-today-start-time-and-current-time-of-est-using-moment-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!