如何使用Moment.js获取今天的EST开始时间和当前时间 [英] How to get today start time and current time of EST using Moment.js

查看:2004
本文介绍了如何使用Moment.js获取今天的EST开始时间和当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

它以毫秒为单位.

是正确还是错误?

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

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

推荐答案

首先,当您使用momentjs时,请显式停止使用Date:

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

这篇关于如何使用Moment.js获取今天的EST开始时间和当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆