解析日期没有时区javascript [英] Parse date without timezone javascript

查看:157
本文介绍了解析日期没有时区javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JavaScript中解析没有时区的日期。我试过:

 新日期(Date.parse(2005-07-08T00:00:00 + 0000)) ; 

返回Fri Jul 08 2005 02:00:00 GMT + 0200 中欧夏令时间

 新日期(Date.parse(2005-07-08 00:00:00 GMT + 0000 )); 

返回相同结果

新日期(Date.parse(2005-07-08 00:00:00 GMT-0000)); 

返回相同结果



我想解析时间:


  1. 没有时区。


  2. 没有调用构造函数Date.UTC或新的日期(年,月,日)。


  3. 只需简单地将字符串传递给Date构造函数(无原型方法) p>


  4. 我必须产品日期对象,而不是 String



解决方案

日期解析正确,只是toString转换它到您当地的时区:

 >新日期(Date.parse(2005-07-08T11:22:33 + 0000))
Fri Jul 08 2005 13:22:33 GMT + 0200(CEST)
> new Date(Date.parse(2005-07-08T11:22:33 + 0000))toUTCString()
Fri,08 Jul 2005 11:22:33 GMT

Javascript日期对象是时间戳 - 它们只包含从时代开始的毫秒数。 Date对象中没有时区信息。这个时间戳表示哪个日历日期(天,分钟,秒)是解释的问题(至... String 方法之一)。



上面的例子显示正在解析日期 - 也就是说,它实际上包含对应于2005-07-08T11:22:33的毫秒数在GMT。


I want to parse date without timezone in JavaScript. I have tried:

new Date(Date.parse("2005-07-08T00:00:00+0000"));

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

Returns same result

new Date(Date.parse("2005-07-08 00:00:00 GMT-0000"));

Returns same result

I want to parse time:

  1. Without time zone.

  2. Without calling constructor Date.UTC or new Date(year, month, day).

  3. Just simple passing string into Date constructor (without prototype approaches).

  4. I have to product Date object, not String.

解决方案

The date is parsed correctly, it's just toString that converts it to your local timezone:

> new Date(Date.parse("2005-07-08T11:22:33+0000"))
Fri Jul 08 2005 13:22:33 GMT+0200 (CEST)
> new Date(Date.parse("2005-07-08T11:22:33+0000")).toUTCString()
"Fri, 08 Jul 2005 11:22:33 GMT"

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆