在UTC timeZONE中将字符串转换为新的Date对象 [英] Convert string to new Date object in UTC timeZONE

查看:354
本文介绍了在UTC timeZONE中将字符串转换为新的Date对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以让我知道如何使用ExtJs中的UTC时区将字符串转换为日期对象

Can anyone let me know how to convert a string to a date Object with UTC time zone in ExtJs?

字符串是2015-10-07T23:59:00。我想在Date对象中得到相同的内容,而不改变时区。

String is "2015-10-07T23:59:00". I would like to get the same in Date Object without changing the timezone.

推荐答案

首先,你的日期字符串没有一个时区。

First of all, your date string does not have a timezone.

当您从字符串中创建JavaScript日期对象时,可能会有两种可能的结果:

When you make a JavaScript date object from a string, there are two possible outcomes you could expect:


  • 您可能希望日期为23:59本地(在我的情况下为23:59 CEST)。

    在这种情况下,您要使用 new Date(2015-10-07 23:59:00)用简单的javascript(注意缺少的 T )或 Ext.Date.parse(2015-10-07T23:59:00,c);

  • 日期为23:59 UTC(例如01:59 CEST)。

    在这种情况下,您要使用新日期(2015-10-07T23:59:00 )

  • You may want the date to be 23:59 Local (23:59 CEST in my case).
    In this case, you want to use new Date("2015-10-07 23:59:00") with plain javascript (note the missing T), or Ext.Date.parse("2015-10-07T23:59:00","c");.
  • You may want the date to be 23:59 UTC (e.g. 01:59 CEST).
    In this case, you want to use new Date("2015-10-07T23:59:00").

当然,每当你输出日期,你必须得到日期正确的时区也是如此。控制台/ toString通常会在当地时间显示。如果您需要其他时区,JavaScript将提供 getUTC ... 方法。

Of course, whenever you output the date, you have to get the date in the correct time zone as well. The console/toString will usually show it in local time. JavaScript does provide getUTC... methods if you require other time zones.

您可以看到,使用时区JavaScript是一个痛苦的经历。如果您需要全职时区支持,我建议您尝试moment.js。

You see, using Time Zones with JavaScript is a painful experience. I would recommend to try moment.js if you need full time zone support.

这篇关于在UTC timeZONE中将字符串转换为新的Date对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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