Author Topic: Javascript Convert String to Date  (Read 12130 times)

Offline admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 296
    • View Profile
Javascript Convert String to Date
« on: April 06, 2010, 02:32:28 PM »
To convert a string in the following formates:
•MM-dd-yyyy
•yyyy/MM/dd
•MM/dd/yyyy
•MMMM dd, yyyy
•MMM dd, yyyy

like:

Variable with Date Format as MM-dd-yyyy:
Code: [Select]
var dateString = "03-20-2008";  // MM-dd-yyyy

Variable with Date Format as yyyy/MM/dd:
Code: [Select]
var dateString = "2008/03/20";  // yyyy/MM/dd
Variable with Date Format as MM/dd/yyyy:
Code: [Select]
var dateString = "03/20/2008";  // MM/dd/yyyy
Variable with Date Format as MMMM dd, yyyy:
Code: [Select]
var dateString = "March 20, 2008";  // MMMM dd, yyyy
Variable with Date Format as MMM dd, yyyy:
Code: [Select]
var dateString = "Mar 20, 2008";  // MMM dd, yyyy

You can use any of the above date string formats that can be passed to the javascript Date Object:
Code: [Select]
var myDate = new Date(dateString);
document.write("Date :" + myDate.getDate());
document.write("<br>");
document.write("Month : " + myDate.getMonth());
document.write("<br>");
document.write("Year : " + myDate.getFullYear());