admin管理员组

文章数量:1389903

i have in 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?

i have in 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?

Share Improve this question edited Nov 6, 2012 at 11:48 raina77ow 107k16 gold badges204 silver badges236 bronze badges asked Nov 6, 2012 at 11:34 Berg SwootsBerg Swoots 1311 silver badge6 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 4

This is a bit more correct:

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

Quoting the Date.parse (MDN) doc:

Alternatively, the date/time string may be in ISO 8601 format. Starting with JavaScript 1.8.5 / Firefox 4, a subset of ISO 8601 is supported. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00 (date and time) can be passed and parsed.

Still, this format is NOT supported by IE8 (as mentioned here). Therefore I suggest at least considering possibility of using external libraries for processing dates - in particular, 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

You have to convert the date string from Y-m-d H:i:s to Y/m/d H:i:s because javascript does not recognize the date string in that format.

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

本文标签: javascriptCreate object Date from text in format Ymd HisStack Overflow