解析没有时区的日期javascript [英] Parse date without timezone javascript
问题描述
我想在 JavaScript 中解析没有时区的日期.我试过了:
I want to parse date without timezone in JavaScript. I have tried:
new Date(Date.parse("2005-07-08T00:00:00+0000"));
返回 2005 年 7 月 8 日星期五 02:00:00 GMT+0200(中欧夏令时)
Returns Fri Jul 08 2005 02:00:00 GMT+0200 (Central European Daylight Time)
new Date(Date.parse("2005-07-08 00:00:00 GMT+0000"));
返回相同的结果
new Date(Date.parse("2005-07-08 00:00:00 GMT-0000"));
返回相同的结果
我想解析时间:
没有时区.
Without time zone.
不调用构造函数 Date.UTC 或 new Date(year, month, day).
Without calling constructor Date.UTC or new Date(year, month, day).
只是简单地将字符串传递给 Date 构造函数(没有原型方法).
Just simple passing string into Date constructor (without prototype approaches).
我必须生成 Date
对象,而不是 String
.
I have to product Date
object, not String
.
推荐答案
日期被正确解析,只是 toString 将其转换为您的本地时区:
The date is parsed correctly, it's just toString that converts it to your local timezone:
let s = "2005-07-08T11:22:33+0000";
let d = new Date(Date.parse(s));
// this logs for me
// "Fri Jul 08 2005 13:22:33 GMT+0200 (Central European Summer Time)"
// and something else for you
console.log(d.toString())
// this logs
// Fri, 08 Jul 2005 11:22:33 GMT
// for everyone
console.log(d.toUTCString())
Javascript Date 对象是时间戳——它们只包含自纪元以来的毫秒数.Date 对象中没有时区信息.这个时间戳代表哪个日历日期(日、分、秒)是一个解释问题(to...String
方法之一).
Javascript Date object are timestamps - they merely contain a number of milliseconds since the epoch. There is no timezone info in a Date object. Which calendar date (day, minutes, seconds) this timestamp represents is a matter of the interpretation (one of to...String
methods).
上面的例子表明日期被正确解析 - 也就是说,它实际上包含对应于格林威治标准时间2005-07-08T11:22:33"的毫秒数.
The above example shows that the date is being parsed correctly - that is, it actually contains an amount of milliseconds corresponding to "2005-07-08T11:22:33" in GMT.
这篇关于解析没有时区的日期javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!