简体   繁体   English

从文本创建对象日期,格式为Ymd H:i:s

[英]Create object Date from text in format Y-m-d H:i:s

i have in jquery: 我在jquery:

var start = '2012-11-10 13:10:13';

Why this not working: 为什么这不起作用:

var date = new Date(start);

? How can i make it without external plugins? 如何在没有外部插件的情况下制作它?

This is a bit more correct: 这更正确一点:

var correctStart = '2012-11-10T13:10:13';
var date = new Date(correctStart);

Quoting the Date.parse ( MDN ) doc: 引用Date.parseMDN )文档:

Alternatively, the date/time string may be in ISO 8601 format. 或者,日期/时间字符串可以是ISO 8601格式。 Starting with JavaScript 1.8.5 / Firefox 4, a subset of ISO 8601 is supported. 从JavaScript 1.8.5 / Firefox 4开始,支持ISO 8601的子集。 For example, "2011-10-10" (just date) or "2011-10-10T14:48:00 (date and time) can be passed and parsed. 例如,可以传递和解析“2011-10-10”(仅日期)或“2011-10-10T14:48:00(日期和时间)”。

Still, this format is NOT supported by IE8 (as mentioned here ). 尽管如此,这种格式不是由IE8的支持(如提到这里 )。 Therefore I suggest at least considering possibility of using external libraries for processing dates - in particular, Moment.js . 因此,我建议至少考虑使用外部库处理日期的可能性 - 特别是Moment.js

Or you Can try it as shown below 或者您可以尝试如下所示

var start = '10/11/2012 13:10:13';    
var date = new Date(start);

This is working 这很有效

Refer this JSFIDDLE DEMO 请参阅此JSFIDDLE DEMO

You have to convert the date string from Ymd H:i:s to Y/m/d H:i:s because javascript does not recognize the date string in that format. 您必须将日期字符串从Ymd H:i:sY/m/d H:i:s因为javascript无法识别该格式的日期字符串。

Do this: 做这个:

var my_date = "2012-11-10 13:10:13";
my_date = my_date.replace(/-/g, "/"); //this will replace "-" with "/"
//alert(my_date);
var  javascript_date = new Date(my_date);

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

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