简体   繁体   English

将字符串转换为 UTC timeZONE 中的新 Date 对象

[英]Convert string to new Date object in UTC timeZONE

Can anyone let me know how to convert a string to a date Object with UTC time zone in ExtJs?谁能告诉我如何在 ExtJs 中将string转换为带有 UTC 时区的date对象?

String is "2015-10-07T23:59:00" .字符串是"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.首先,您的日期字符串没有时区。

When you make a JavaScript date object from a string, there are two possible outcomes you could expect:当您从字符串创建 JavaScript 日期对象时,您可以预期有两种可能的结果:

  • You may want the date to be 23:59 Local (23:59 CEST in my case).您可能希望日期为本地 23:59(在我的情况下为 23:59 CEST)。
    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");在这种情况下,您想使用带有纯 javascript 的new Date("2015-10-07 23:59:00") (注意缺少的T ),或Ext.Date.parse("2015-10-07T23:59:00","c"); . .
  • You may want the date to be 23:59 UTC (eg 01:59 CEST).您可能希望日期为 23:59 UTC(例如 01:59 CEST)。
    In this case, you want to use new Date("2015-10-07T23:59:00") .在这种情况下,您要使用new Date("2015-10-07T23:59:00")

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. console/toString 通常会在本地时间显示它。 JavaScript does provide getUTC... methods if you require other time zones.如果您需要其他时区,JavaScript 确实提供了getUTC...方法。

You see, using Time Zones with JavaScript is a painful experience.你看,在 JavaScript 中使用时区是一种痛苦的体验。 I would recommend to try moment.js if you need full time zone support.如果您需要全时区支持,我建议您尝试使用 moment.js。

You can use Ext.Date.parse .It gives Date Object as output.It syntax is:您可以使用Ext.Date.parse它提供日期对象作为输出。它的语法是:

Ext.Date.parse( String input, String format, [Boolean strict] )

For Example:例如:

Ext.Date.parse("2015-10-07T23:59:00", "Y-m-dTH:i:s");

try尝试

var millisFromEpoch = Date.parse('2015-10-07T23:59:00');

it will parse date in GMT timezone, Ext.date.parse use the current timezone instead它将解析 GMT 时区中的日期,Ext.date.parse 使用当前时区代替

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM