试图将时间转换为EST时区

trying to convert time into EST time zone

本文关键字:EST 时区 转换 时间      更新时间:2023-09-26

尝试用以下代码将本地日期时间转换为EST时间,并获得EST时间,但它少了一个小时,例如,如果它是6.00 EST,它会显示5.00 EST。有人知道出了什么问题吗?

var offset = -5.0;
var fromDate = new Date(selectedDate); // selected date is String of date which is set currently
var utcFromTime = fromDate.getTime() + (fromDate.getTimezoneOffset() * 60000);
var effFromDate = new Date(utcFromTime + (3600000 * offset));
alert("final EST :" + effFromDate);

因此,使用即时时区可以执行

//create new moment with local time then update the timezone
var localTimeInMurica = moment().tz("EST");

//display EST time of the local time orgionally passed
//see http://momentjs.com/docs/#/displaying/ for options on displaying the date
console.log(localTimeInMurica.format());

这是一个fiddle(我手动将即时时区包括在中,而不是通过cdn,因为我想获得所有年份的最新信息)http://fiddle.jshell.net/leighking2/61j008n0/

用于EDT

//create new moment with local time then update the timezone
var localTimeInMurica = moment().tz("America/New_York");

//display EST time of the local time orgionally passed
//see http://momentjs.com/docs/#/displaying/ for options on displaying the date
console.log(localTimeInMurica.format());

很抱歉,我认为您不能使用JavaScript来实现这一点,因为它不处理时区。您应该使用诸如PHP之类的语言,并使用Ajax将结果返回到JavaScript。

使用此链接

var UTC = new Date();
var UTC = UTC.getTime() // Get UTC Timestamp

通过设置时区的小时和分钟

var IST = new Date(date); // Clone UTC Timestamp
IST.setHours(IST.getHours() + 5); // set Hours to 5 hours later
IST.setMinutes(IST.getMinutes() + 30); // set Minutes to be 30 minutes later

或使用moment.js

var dec = moment("2014-12-01T12:00:00Z");
dec = dec.tz('America/New_York');