我正在寻找一个函数转换日期在一个时区到另一个。
它需要两个参数,
日期(格式为“2012/04/10 10:10:30 +0000”) 时区字符串("Asia/Jakarta")
时区字符串在http://en.wikipedia.org/wiki/Zone.tab中描述
有什么简单的方法吗?
我正在寻找一个函数转换日期在一个时区到另一个。
它需要两个参数,
日期(格式为“2012/04/10 10:10:30 +0000”) 时区字符串("Asia/Jakarta")
时区字符串在http://en.wikipedia.org/wiki/Zone.tab中描述
有什么简单的方法吗?
当前回答
下面是一行代码:
function convertTZ(date, tzString) { return new Date((typeof date === "string" ? new Date(date) : date).toLocaleString("en-US", {timeZone: tzString})); } // usage: Asia/Jakarta is GMT+7 convertTZ("2012/04/20 10:10:30 +0000", "Asia/Jakarta") // Tue Apr 20 2012 17:10:30 GMT+0700 (Western Indonesia Time) // Resulting value is regular Date() object const convertedDate = convertTZ("2012/04/20 10:10:30 +0000", "Asia/Jakarta") convertedDate.getHours(); // 17 // Bonus: You can also put Date object to first arg const date = new Date() convertTZ(date, "Asia/Jakarta") // current date-time in jakarta.
这是MDN参考。
注意:上面的函数依赖于解析toLocaleString结果,这是一个以en-US语言环境格式化的日期字符串,例如。“4/20/2012,下午5:10:30”。每个浏览器可能不接受en-US格式的日期字符串到它的日期构造函数,它可能返回意想不到的结果(它可能忽略夏令时)。
目前所有现代浏览器都接受这种格式并正确计算夏令时,它可能不适用于旧浏览器和/或外来浏览器。
旁注:如果现代浏览器有tolocedate,那就太好了 函数,所以我们不需要使用这个俗套的方法。
其他回答
我在使用瞬间时区时遇到了麻烦。我加上这个答案是为了让其他人面对同样的问题。所以我有一个日期字符串2018-06-14 13:51:00来自我的API。我知道这是存储在UTC,但字符串本身并不说话。
我让moment timezone知道这个日期来自哪个时区:
let uTCDatetime = momentTz.tz("2018-06-14 13:51:00", "UTC").format();
// If your datetime is from any other timezone then add that instead of "UTC"
// this actually makes the date as : 2018-06-14T13:51:00Z
现在我想通过这样做将其转换为特定的时区:
let dateInMyTimeZone = momentTz.tz(uTCDatetime, "Asia/Kolkata").format("YYYY-MM-DD HH:mm:ss");
// now this results into: 2018-06-14 19:21:00, which is the corresponding date in my timezone.
我不知道一个简单的方法来转换日期对象到任何时区,但如果你想把它转换到本地时区,你可以用date .prototype. gettime()转换到相应的毫秒数,然后再回来。
let date0 = new Date('2016-05-24T13:07:20'); let date1 = new Date(date0.getTime()); console.log(“$ {date0} \ n $ {date1} ');
例如,date.getHours()现在将返回15而不是13,如果你像我一样在奥地利(而且是夏天)。
我读到过,各种datetime函数在某些浏览器中可能会表现出非标准的行为,所以先测试一下。我可以确认它在Chrome中工作。
只需设置你想要的国家时区,你可以很容易地在html中显示它更新使用setinterval()函数后每一分钟。函数formatAMPM()管理12小时格式和AM/PM时间显示。
$(document).ready(function(){
var pakTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Karachi"});
pakTime = new Date(pakTime);
var libyaTime = new Date().toLocaleString("en-US", {timeZone: "Africa/Tripoli"});
libyaTime = new Date(libyaTime);
document.getElementById("pak").innerHTML = "PAK "+formatAMPM(pakTime);
document.getElementById("ly").innerHTML = "LY " +formatAMPM(libyaTime);
setInterval(function(today) {
var pakTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Karachi"});
pakTime = new Date(pakTime);
var libyaTime = new Date().toLocaleString("en-US", {timeZone: "Africa/Tripoli"});
libyaTime = new Date(libyaTime);
document.getElementById("pak").innerHTML = "PAK "+formatAMPM(pakTime);
document.getElementById("ly").innerHTML = "LY " +formatAMPM(libyaTime);
},10000);
function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
});
好了,找到了!
我使用的是timezone-js。这是代码:
var dt = new timezoneJS.Date("2012/04/10 10:10:30 +0000", 'Europe/London');
dt.setTimezone("Asia/Jakarta");
console.debug(dt); //return formatted date-time in asia/jakarta
你也可以使用 https://www.npmjs.com/package/ctoc_timezone
它有很多简单的实现和格式定制。
更改toTimeZone格式:
中华奥。toTimeZone(new Date(),"EST","Do MMM YYYY hh:mm:ss #{EST}");
输出:
2013年2月28日19:00:00 EST
您可以在文档中探索多种功能。