0

Hey everyone,

Brand new to javascript and had a, hopefully, quick question.

I have pulled a date from our database in the form of mm/dd/yyyy, and would like to convert this to a Date object type. To do this, I have parsed the string to switch it to the following format yyyy,mm,dd.

I have already created a new Date variable using the following:

Date reqDate = new Date(2010,10,14)

and had no issues.

However, if I do the following:

Date reqConvert = new Date (reqDate3)

where reqdate3 = 2010,11,15
I get the following error:
"Invalid Date". If anyone can shed some light on this, I would greatly appreciate it.

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Airshow
0

Winky,

I expect your reqDate3 is "2010,11,15" which is a string of digits and commas.

Passing this string to new Date() is not the same as new Date(2010,11,15) , in which three numbers are passed.

Starting with d = "11/15/2010" , you create a date as follows:

var d = "11/15/2010";//the ubiquitous but completely non-sensical mm/dd/yyyy format
var dArray = d.split('/');//creates the array [11,15,2010]
var dateObj = new Date(dArray[3], dArray[0]-1, dArray[2]);

Why -1 for the month? Because Date's months run from January:0 to December:11;

Days and years are straightforward Gregorian values as per diaries and calendars.

Airshow

0

Correction

var d = "11/15/2010";//the ubiquitous but completely non-sensical mm/dd/yyyy format
var dArray = d.split('/');//creates the array [11,15,2010]
var dateObj = new Date(dArray[2], dArray[0]-1, dArray[1]);

Airshow

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.