String to Date

Results 1 to 2 of 2

Thread: String to Date

  1. #1
    Join Date
    Dec 1969

    Default String to Date

    I have a Javascript string variable that I need to convert into a date so that I can use it in a SQL select statement. The string is a concatenation of some user input and it is presented in dd/mm/yy format yet it is not being recognised as a date. I'm sure that there must be a simple answer but I just don't know it! Please help!

  2. #2
    Dan Evans Guest

    Default RE: String to Date

    If you want to convert a string to a JavaScript Date object, just do this:<BR><BR>var str = "04/03/2000"<BR>var d = new Date(str);<BR><BR>However, depending on locale, this could be read as the 4th of March or the 3rd of April. It&#039;s safer therefore to do this:<BR><BR>d.setDate(str.substring(0, 2)); //First 2 digits<BR>d.setMonth(str.substring(3, 5) - 1); //Middle 2 (Notice that January is 0 not 1 so we subtract 1)<BR>d.setYear(str.substring(6, 8)); //Last 2<BR><BR>One final point, my IE regards &#039;01 as 1901 so you might want to bump the year up to 4 digits.<BR><BR>Hope this helps,<BR><BR>-Dan Evans

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts