我正在寻找一个函数转换日期在一个时区到另一个。
它需要两个参数,
日期(格式为“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中描述
有什么简单的方法吗?
当前回答
我应该指出,我在可以使用的外部库方面受到了限制。moment.js和timezone-js不是我的选择。
我拥有的js日期对象是UTC。我需要在特定的时区(在我的例子中是“America/Chicago”)从这个日期获得日期和时间。
var currentUtcTime = new Date(); // This is in UTC
// Converts the UTC time to a locale specific format, including adjusting for timezone.
var currentDateTimeCentralTimeZone = new Date(currentUtcTime.toLocaleString('en-US', { timeZone: 'America/Chicago' }));
console.log('currentUtcTime: ' + currentUtcTime.toLocaleDateString());
console.log('currentUtcTime Hour: ' + currentUtcTime.getHours());
console.log('currentUtcTime Minute: ' + currentUtcTime.getMinutes());
console.log('currentDateTimeCentralTimeZone: ' + currentDateTimeCentralTimeZone.toLocaleDateString());
console.log('currentDateTimeCentralTimeZone Hour: ' + currentDateTimeCentralTimeZone.getHours());
console.log('currentDateTimeCentralTimeZone Minute: ' + currentDateTimeCentralTimeZone.getMinutes());
国际标准时间目前比“美国/芝加哥”早6小时。输出是:
currentUtcTime: 11/25/2016
currentUtcTime Hour: 16
currentUtcTime Minute: 15
currentDateTimeCentralTimeZone: 11/25/2016
currentDateTimeCentralTimeZone Hour: 10
currentDateTimeCentralTimeZone Minute: 15
其他回答
如果你不想导入一些大的库,你可以使用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')
无耻地窃取自: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();
}
这个函数通过提供城市/国家的名称和偏移值来计算时区值
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);
熟悉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,似乎不值得信赖。
设置一个变量,用-符号分隔年、月和日,加上一个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 );