How to get today start time and current time of ES

2019-07-28 07:04发布

问题:

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