jQuery DateTime 可以解析这个日期吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16983229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:12:15  来源:igfitidea点击:

Can jQuery DateTime parse this date?

javascriptjquerydatepicker

提问by More Than Five

I have a datetime of the form:

我有一个日期时间形式:

var myDate = "2013-06-07T00:00:00.000Z"

I wish to do

我想做

jQuery.datepicker.parseDate( "yy-mm-dd", myDate);

I don't care about the time part.

我不在乎时间部分。

I get:

我得到:

"Extra/unparsed characters found in date: T00:00:00.000Z"

Best I got so far is:

到目前为止我得到的最好的是:

myDate = myDate.replace('T00:00:00.000Z', '');
myDate = jQuery.datepicker.parseDate("yy-mm-dd", myDate).toUTCString();

Please help.

请帮忙。

回答by Arun P Johny

As it is ISO date format, I think you can call new Date(myDate)directly there is no need to parse it I think

由于它是ISO日期格式,我认为您可以new Date(myDate)直接调用没有必要解析它我认为

var date = new Date(myDate);

回答by Thomas Junk

If you don't care about the time part, why not simply

如果你不在乎时间部分,为什么不干脆

jQuery.datepicker.parseDate( "yy-mm-dd", myDate.split("T")[0]);

Perhaps for general DateTime handling, have a look at moment.js

也许对于一般的 DateTime 处理,请查看 moment.js

回答by metalfight - user868766

You can use split

您可以使用拆分

var myDate = "2013-06-07T00:00:00.000Z";
var n=myDate.split("T");
console.log(n); // Pass the date part only to date picker

回答by Paul S.

By modifying the Stringused to describe the format, you can get it to do this (assuming time is always zero)

通过修改用于描述格式的字符串,您可以让它做到这一点(假设时间始终为零)

var myDate = "2013-06-07T00:00:00.000Z",
    d = jQuery.datepicker.parseDate("yy-mm-ddT00:00:00.000Z", myDate);
d; // Fri Jun 07 2013 00:00:00 GMT+0100 (GMT Daylight Time)

However, this will ignore the fact that Zdenotes timezone UTCand instead uses local timezone (in my case BST/GMT+1). You can repair this quickly though.

但是,这将忽略Z表示时区的事实,UTC而是使用本地时区(在我的情况下 BST/ GMT+1)。不过,您可以快速修复此问题。

d.setMinutes(d.getMinutes() - d.getTimezoneOffset()); // or use UTCMinutes
d; // Thu Jun 07 2013 01:00:00 GMT+0100 (GMT Daylight Time)
// which is now correct in terms of timezone

回答by JohnWolf

If you want something that will change your life, just use http://momentjs.com/... It's the equivalent of Carbon for PHP. Simple, multi-language and cross-browsers.

如果你想要改变你生活的东西,只需使用http://momentjs.com/......它相当于 PHP 的 Carbon。简单、多语言和跨浏览器。

For your question, it goes :

对于你的问题,它是:

moment("2013-06-07T00:00:00.000Z").format('YY-MM-DD')

Hope this helps future visitors.

希望这对未来的游客有所帮助。

回答by kav

What about this:

那这个呢:

jQuery.datepicker.parseDate( "yy-mm-dd", myDate.substr(0, 10) );