how do you create a date in javascript from an input box?

Question by SelArom: how do you create a date in javascript from an input box?
I have a text input box with a date in this format:

yyyy-mm-dd

like say it might load with the date 2007-04-01 for april 1, 2007…

what I want to do is create a javascript function or snippet that will create a date from this, so that I can pass a valid javascript date to another function. I don’t know enough about javascript to do this, so any help would be appreciated.

thanks!
-SelArom

Best answer:

Answer by rod
The JavaScript ‘Date’ function reads in a Month first, Day second, Year third format. So you’ll need to grab your string and put the parts into variables.

Then use the Date function like:

MyDate = new Date(strMonth, strDay, strYear) or
MyDate = new Date(04,01,2007)

Know better? Leave your own answer in the comments!

Revisions

There are no revisions for this post.

Tags: , , , ,

One Response to “how do you create a date in javascript from an input box?”

  1. AQuestionMark August 9, 2011 at 5:30 PM #

    1. grab the entry from the text box
    2. split the entry into 3 array elements using string.split()
    3. make a date object boxdate = new Date();
    4. set year, month, date by dateObj.setYear(),dateObj.setMonth() and dateObj.setDate()
    5. then the dateObj has its date set and ready for further process
    Here’s a demo http://www.freewebs.com/swhtam/YA/boxDate.html will full annotation illustrates the above discussion.
    Note: Date validation of input is another issue not included in above.

Leave a Reply