最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get today start time and current time of EST using Moment.js - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Feb 12, 2016 at 16:16 Marcelo 4,4551 gold badge19 silver badges30 bronze badges asked Feb 12, 2016 at 10:44 PrabuPrabu 1653 silver badges14 bronze badges 3
  • why not use the Date object? I dont think you need moment for this. What do you trying to do?? – Alon Commented Feb 12, 2016 at 10:47
  • if I use Date object it will give current timezone time but I need only EST time zone so I am using moment.js – Prabu Commented Feb 12, 2016 at 10:58
  • 2 OK.. this is not reason not use date.. – Alon Commented Feb 12, 2016 at 11:03
Add a ment  | 

1 Answer 1

Reset to default 7

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);
发布评论

评论列表(0)

  1. 暂无评论