我正在寻找一个函数转换日期在一个时区到另一个。

它需要两个参数,

日期(格式为“2012/04/10 10:10:30 +0000”) 时区字符串("Asia/Jakarta")

时区字符串在http://en.wikipedia.org/wiki/Zone.tab中描述

有什么简单的方法吗?


当前回答

设置一个变量,用-符号分隔年、月和日,加上一个T和HH:mm:ss模式的时间,在字符串末尾加上+01:00(在我的例子中,时区是+1)。然后使用此字符串作为日期构造函数的参数。

// desired format: 2001-02-04T08:16:32+01:00
dateAndTime = year+"-"+month+"-"+day+"T"+hour+":"+minutes+":00+01:00";

var date = new Date(dateAndTime );

其他回答

得到它!

希望强制显示的日期=服务器日期,无论本地设置(UTC)。

我的服务器是GMT-6——> new Date().getTimezoneOffset() = 360

myTZO = 360;
myNewDate = new Date(myOldDateObj.getTime() + (60000*(myOldDateObj.getTimezoneOffset()-myTZO)));
alert(myNewDate);

无耻地窃取自:http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329

/** 
 * function to calculate local time
 * in a different city
 * given the city's UTC offset
 */
function calcTime(city, offset) {

    // create Date object for current location
    var d = new Date();
   
    // get UTC time in msec
    var utc = d.getTime();
   
    // create new Date object for different city
    // using supplied offset
    var nd = new Date(utc + (3600000*offset));
   
    // return time as a string
    return "The local time in " + city + " is " + nd.toLocaleString();
}

这个函数通过提供城市/国家的名称和偏移值来计算时区值

你可以使用to toLocaleString()方法来设置时区。

new Date().toLocaleString('en-US', { timeZone: 'Indian/Christmas' })

对于印度,你可以使用“印度/圣诞节”,以下是不同的时区,

"Antarctica/Davis",
    "Asia/Bangkok",
    "Asia/Hovd",
    "Asia/Jakarta",
    "Asia/Phnom_Penh",
    "Asia/Pontianak",
    "Asia/Saigon",
    "Asia/Vientiane",
    "Etc/GMT-7",
    "Indian/Christmas"

如果你不想导入一些大的库,你可以使用Intl。DateTimeFormat将Date对象转换为不同的时区。

// Specifying timeZone is what causes the conversion, the rest is just formatting const options = { year: '2-digit', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', timeZone: 'Asia/Jakarta', timeZoneName: 'short' } const formatter = new Intl.DateTimeFormat('sv-SE', options) const startingDate = new Date("2012/04/10 10:10:30 +0000") const dateInNewTimezone = formatter.format(startingDate) console.log(dateInNewTimezone) // 12-04-10 17:10:30 GMT+7

补偿,夏令时,和过去的变化将为您照顾。

更新

还有一个新的时态工具,可以处理时区和其他事情。比如只有日期或时间。目前还处于试验阶段

这是为了取代旧的遗产日期

var isoDate = new Date().toJSON() // eg: '2022-11-18T13:56:09.697Z'
Temporal.Instant.from(isoDate).toZonedDateTimeISO('Europe/Stockholm')

环顾四周,包括这个页面的链接,我发现了这篇很棒的文章,使用moment timezone:

https://www.webniraj.com/2016/11/23/javascript-using-moment-js-to-display-dates-times-in-users-timezone/

总结一下:

获取用户的时区

var tz = moment.tz.guess();
console.info('Timezone: ' + tz);

返回时区:欧洲/伦敦

设置默认用户时区

moment.tz.setDefault(tz);

设置自定义时区

moment.tz.setDefault('America/Los_Angeles');

将日期/时间转换为本地时区,假设原始日期/时间为UTC

moment.utc('2016-12-25 07:00').tz(tz).format('ddd, Do MMMM YYYY, h:mma');

返回时间:2016年12月25日,星期日,上午7:00

将日期/时间转换为洛杉矶时间

moment.utc('2016-12-25 07:00').tz('America/Los_Angeles').format('ddd, Do MMMM YYYY, h:mma');

返回时间:2016年12月24日星期六晚上11:00

将洛杉矶时间转换为伦敦时间

moment.tz('2016-12-25 07:00', 'America/Los_Angeles').tz('Europe/London').format( 'ddd, Do MMMM YYYY, h:mma' );

返回时间:2016年12月25日,星期日,下午3:00