1

I have string like "02.09.13"'

I want to format this sting to date. For this i have written code like,

var date = new Date("02.09.13");
alert(date);

It is printing 'NaN'

but this code is working if the string like "02/09/12"

Niranjan Reddy
  • 23
  • 3
  • 12
  • 1
    Try This:- http://stackoverflow.com/questions/18572199/change-date-format-who-is-stored-in-text-field/18572757#18572757 – Vikash Pandey Sep 20 '13 at 04:49

4 Answers4

4

For All browsers apart from IE (not sure which versions) your code will work

var date = new Date('02.09.13');
alert(date);

for IE we use :

var date = new Date(Date.parse('02.09.13'.replace(/\./ig, '/')));
alert(date);

If you actually want to parse a date using the format 'dd.mm.yy' It's worth noting the Date object does not do that out of the box. There is a really good Library called moment.js which makes parsing these kind of dates super simple, example below for your date:

moment("02.09.13", "DD.MM.YY");
Yussuf S
  • 2,022
  • 1
  • 14
  • 12
0

NaN show only on iE browser where you need correct date time format.

var d = new Date(2011, 01, 07); // yyyy, mm-1, dd  
var d = new Date(2011, 01, 07, 11, 05, 00); // yyyy, mm-1, dd, hh, mm, ss  
var d = new Date("02/07/2011"); // "mm/dd/yyyy"  
var d = new Date("02/07/2011 11:05:00"); // "mm/dd/yyyy hh:mm:ss"  
var d = new Date(1297076700000); // milliseconds  
var d = new Date("Mon Feb 07 2011 11:05:00 GMT"); // ""Day Mon dd yyyy hh:mm:ss GMT/UTC 

Check this blog: JavaScript new Date() Returning NaN in IE or Invalid Date in Safari

Best Regards

BizApps
  • 6,048
  • 9
  • 40
  • 62
0

havent tried new Date("02.09.13".replace(/\./g, '/')); // but thinks 13 ~ 1913

For Date {Sat Feb 09 2013 00:00:00 GMT-0800 (Pacific Daylight Time)}

havent tried new Date("02.09.2013".replace(/\./g, '/'));

internals-in
  • 4,798
  • 2
  • 21
  • 38
0

You need to replace the . with / Like the one given below

var unformattedDate= "02.09.13"
var actDate = unformattedDate.replace(/\./g, '/');

var date = new Date( actDate );
alert(date);

Hope this may help.

Abdul Hamid
  • 3,222
  • 3
  • 24
  • 31