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.

Recommended Answers

All 2 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.