我正在寻找一个函数转换日期在一个时区到另一个。
它需要两个参数,
日期(格式为“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中描述
有什么简单的方法吗?
当前回答
你也可以尝试将日期时区转换为印度:
var indianTimeZoneVal = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'});
var indainDateObj = new Date(indianTimeZoneVal);
indainDateObj.setHours(indainDateObj.getHours() + 5);
indainDateObj.setMinutes(indainDateObj.getMinutes() + 30);
console.log(indainDateObj);
其他回答
This is UTC date; converting UTC to IST timezone; let sampleArray = [ { date: "2022-12-22T19:16:26.803" }, { date: "2022-12-22T19:16:26.77" }, { date: "2022-12-22T19:16:26.737" }, { date: "2022-12-22T19:16:26.72" } ]; // Get all the results whose boolresult is 'true' // solution 1 sampleArray.map((element) => { let utcDate = new Date(element.date).getTime(); let dateIST = new Date(utcDate); dateIST.setHours(dateIST.getHours() + 5); dateIST.setMinutes(dateIST.getMinutes() + 30); element.date = dateIST; }); console.log("Result ==>>", sampleArray); // solution 2 sampleArray.map((element) => { element.date = new Date(element.date).toLocaleString("en-US", { timeZone: "Asia/Kolkata" }); }); console.log("Result 2==>>", sampleArray);
有一个npm模块叫做timezones。Json你可以使用它。它基本上由一个json文件和包含夏令时和偏移量信息的对象组成。
对于asia/jakarta,它可以返回这个对象:
{
"value": "SE Asia Standard Time",
"abbr": "SAST",
"offset": 7,
"isdst": false,
"text": "(UTC+07:00) Bangkok, Hanoi, Jakarta",
"utc": [
"Antarctica/Davis",
"Asia/Bangkok",
"Asia/Hovd",
"Asia/Jakarta",
"Asia/Phnom_Penh",
"Asia/Pontianak",
"Asia/Saigon",
"Asia/Vientiane",
"Etc/GMT-7",
"Indian/Christmas"
]
}
你可以在这里找到它:
https://github.com/dmfilipenko/timezones.json
https://www.npmjs.com/package/timezones.json
希望对大家有用
只需设置你想要的国家时区,你可以很容易地在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;
}
});
下面是一行代码:
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,那就太好了 函数,所以我们不需要使用这个俗套的方法。
快速和肮脏的手动换时和返回:
return new Date(new Date().setHours(new Date().getHours()+3)).getHours()