Riz*_*han 294 javascript timezone
我正在寻找一个功能,将日期在一个时区转换为另一个时区.
它需要两个参数,
时区字符串在http://en.wikipedia.org/wiki/Zone.tab中描述
是否有捷径可寻?
Bri*_*lma 128
对于moment.js用户,您现在可以使用moment-timezone.使用它,你的功能看起来像这样:
function toTimeZone(time, zone) {
var format = 'YYYY/MM/DD HH:mm:ss ZZ';
return moment(time, format).tz(zone).format(format);
}
Run Code Online (Sandbox Code Playgroud)
Udh*_*aya 107
var aestTime = new Date().toLocaleString("en-US", {timeZone: "Australia/Brisbane"});
aestTime = new Date(aestTime);
console.log('AEST time: '+aestTime.toLocaleString())
var asiaTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Shanghai"});
asiaTime = new Date(asiaTime);
console.log('Asia time: '+asiaTime.toLocaleString())
var usaTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});
usaTime = new Date(usaTime);
console.log('USA time: '+usaTime.toLocaleString())
var indiaTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Kolkata"});
indiaTime = new Date(indiaTime);
console.log('India time: '+indiaTime.toLocaleString())Run Code Online (Sandbox Code Playgroud)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
Val*_*i69 89
/**
* 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();
// convert to msec
// add local time zone offset
// get UTC time in msec
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// 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();
}
Run Code Online (Sandbox Code Playgroud)
此功能通过提供城市/国家/地区的名称和偏移值来计算时区值
lam*_*tor 86
除Safari之外的大多数桌面(非移动)浏览器都支持带参数的toLocaleString函数,旧浏览器通常会忽略这些参数.
new Date().toLocaleString('en-US', { timeZone: 'Asia/Jakarta' })
Run Code Online (Sandbox Code Playgroud)
Riz*_*han 57
好的,找到了!
我正在使用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
Run Code Online (Sandbox Code Playgroud)
Ced*_*mon 10
得到它了 !
想要强制显示的日期=服务器日期,不要使用本地设置(UTC).
我的服务器是GMT-6 - > new Date().getTimezoneOffset()= 360.
myTZO = 360;
myNewDate=new Date(myOldDateObj.getTime() + (60000*(myOldDateObj.getTimezoneOffset()-myTZO)));
alert(myNewDate);
Run Code Online (Sandbox Code Playgroud)
End*_*ess 10
如果您不想导入一些大型库,则可以使用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 formater = new Intl.DateTimeFormat('sv-SE', options)
const startingDate = new Date("2012/04/10 10:10:30 +0000")
const dateInNewTimezone = formater.format(startingDate)
console.log(dateInNewTimezone) // 12-04-10 17:10:30 GMT+7Run Code Online (Sandbox Code Playgroud)
偏移量,夏时制和过去的更改将为您解决。
您可以使用 toLocaleString() 方法来设置时区。
new Date().toLocaleString('en-US', { timeZone: 'Indian/Christmas' })
Run Code Online (Sandbox Code Playgroud)
对于印度,您可以使用“Indian/Christmas”,以下是不同的时区,
"Antarctica/Davis",
"Asia/Bangkok",
"Asia/Hovd",
"Asia/Jakarta",
"Asia/Phnom_Penh",
"Asia/Pontianak",
"Asia/Saigon",
"Asia/Vientiane",
"Etc/GMT-7",
"Indian/Christmas"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
483269 次 |
| 最近记录: |