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

它需要两个参数,

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

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

有什么简单的方法吗?


当前回答

所有这些答案都有点多余,但这对我来说是有效的,可以获得具有特定小时偏移量的当前Date对象。

function hourToMs(hour) { return hour * 60 * 1000 * 60; } function minToMs(min) { return min * 60 * 1000; } function getCurrentDateByOffset(offset) { // Get the current timezone in milliseconds to reset back to GMT aka +0 let timezoneOffset = minToMs((new Date()).getTimezoneOffset()); // get the desired offset in milliseconds, invert the value because javascript is dum let desiredOffset = hourToMs(offset * -1); return new Date(Date.now() + timezoneOffset - desiredOffset); } // -6 hours is central timezone console.log("The time is: " + getCurrentDateByOffset(-6));

其他回答

熟悉java 8的人java。Time包,或者joda-time可能会喜欢这个新产品:js-joda库。

安装

npm install js-joda js-joda-timezone --save

例子

<script src="node_modules/js-joda/dist/js-joda.js"></script>
<script src="node_modules/js-joda-timezone/dist/js-joda-timezone.js"></script>
<script>
var dateStr = '2012/04/10 10:10:30 +0000';
JSJoda.use(JSJodaTimezone);
var j = JSJoda;
// https://js-joda.github.io/js-joda/esdoc/class/src/format/DateTimeFormatter.js~DateTimeFormatter.html#static-method-of-pattern
var zonedDateTime = j.ZonedDateTime.parse(dateStr, j.DateTimeFormatter.ofPattern('yyyy/MM/dd HH:mm:ss xx'));
var adjustedZonedDateTime = zonedDateTime.withZoneSameInstant(j.ZoneId.of('America/New_York'));
console.log(zonedDateTime.toString(), '=>', adjustedZonedDateTime.toString());
// 2012-04-10T10:10:30Z => 2012-04-10T06:10:30-04:00[America/New_York]
</script>

在真正的java本质中,这是相当啰嗦的。但是,作为一个移植的java库,特别是考虑到他们移植了1800个左右的测试用例,它也可能非常准确地工作。

时间操作是很难的。这就是为什么许多其他库在边缘情况下存在bug。Moment.js的时区似乎是正确的,但我所见过的其他js库,包括时区-js,似乎不值得信赖。

服务器有问题,选择gmt+0000标准时区,你可以在javascript中使用moment-timezone库来更改它

const moment = require("moment-timezone")
const dateNew = new Date()
const changeZone = moment(dateNew);
changeZone.tz("Asia/Karachi").format("ha z");
// here you can paste "your time zone string"

你也可以尝试将日期时区转换为印度:

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);

你也可以使用 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

您可以在文档中探索多种功能。

这是我的代码,它工作得很完美,你可以试试下面的演示:

$(document).ready(function() { //EST setInterval( function() { var estTime = new Date(); var currentDateTimeCentralTimeZone = new Date(estTime.toLocaleString('en-US', { timeZone: 'America/Chicago' })); var seconds = currentDateTimeCentralTimeZone.getSeconds(); var minutes = currentDateTimeCentralTimeZone.getMinutes(); var hours = currentDateTimeCentralTimeZone.getHours()+1;//new Date().getHours(); var am_pm = currentDateTimeCentralTimeZone.getHours() >= 12 ? "PM" : "AM"; if (hours < 10){ hours = "0" + hours; } if (minutes < 10){ minutes = "0" + minutes; } if (seconds < 10){ seconds = "0" + seconds; } var mid='PM'; if(hours==0){ //At 00 hours we need to show 12 am hours=12; } else if(hours>12) { hours=hours%12; mid='AM'; } var x3 = hours+':'+minutes+':'+seconds +' '+am_pm // Add a leading zero to seconds value $("#sec").html(x3); },1000); }); <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body> <p class="date_time"><strong id="sec"></strong></p> </body> </html>