Anirban Ghosh
Anirban Ghosh

Reputation: 125

Javascript Jquery Date "Invalid Date" Error

I am fetching a record from jquery grid- jqxgrid, when data record is fetched, trying to have operation on a date time column and display the date only, not date along with time

Please see the following code:

if ((dataRecord.ReportNextRun.toString().replace(/\s/g, '')) != "") {

    alert(dataRecord.ReportNextRun); //11/08/2012 12:09AM
    alert("Hiiiiiiiiii");
    var d = new Date(dataRecord.ReportNextRun);
    alert(d); // Invalid Date
    var curr_date = d.getDate();
    alert(curr_date); // NaN
    var curr_month = d.getMonth(dataRecord.ReportNextRun) + 1; //Months are zero based
    var curr_year = d.getFullYear(dataRecord.ReportNextRun);
    alert(curr_date + "-" + curr_month + "-" + curr_year); // NaN-NaN-NaN
}​

Please see the comments after the line that I am getting and suggest what to do.

Upvotes: 0

Views: 2483

Answers (1)

Shmiddty
Shmiddty

Reputation: 13967

The problem appears to be this:

11/08/2012 12:09AM
                ^ 

Should be

11/08/2012 12:09 AM
                 ^ 

The javascript date parser (for Chrome, at least) doesn't recognize the first value as a date string.

See this demo: http://jsfiddle.net/RY99T/1/

Upvotes: 1

Related Questions