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

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

本文关键字:时间 今天开始 EST 何使用 Moment js 获取      更新时间:2023-09-26

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

    开始时间
  • 将是今天的开始时间
  • 结束时间将是当前时间。

我用过瞬间.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"));

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

是对还是错?

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

首先,当你使用 momentjs 时,停止明确使用 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);