stuart-d2
3/10/2016 - 4:06 PM

Javascript DATE and TIME functions : Creation and Conversion

Javascript DATE and TIME functions : Creation and Conversion

var myNewDate = new Date(Date());
//Output : undefined
myNewDate
//Output : Thu Mar 10 2016 09:57:10 GMT-0600 (Central Standard Time)
myNewDate instanceof Date;
//Output : true
var myNewDateString = myNewDate.toISOString();
//Output : undefined
myNewDateString
//Output : "2016-03-10T15:57:10.000Z"
typeof(myNewDateString)==="string";
//Output : true

/*2. Some various conversions Tested */  
//Note, this is CONSOLE input/Output
      
      //current date
      var currentDate = new Date();
      undefined
      currentDate
      Tue Mar 15 2016 14:01:26 GMT-0500 (Central Daylight Time)
      
      //convering miliseconds to a date
      currentDateA = new Date(1458068173454);
      Tue Mar 15 2016 13:56:13 GMT-0500 (Central Daylight Time)
      currentDateA
      Tue Mar 15 2016 13:56:13 GMT-0500 (Central Daylight Time)
      
      //converting a date into milliseconds 
      currentDateMilliseconds = Date.now(currentDateA);
      1458070121564
      currentDateMilliseconds
      1458070121564
      
      //to UTC string
      currentDateUTC = currentDate.toUTCString();
      "Tue, 15 Mar 2016 19:01:26 GMT"
      
      //To your garden variety date string 
      currentDateString = currentDate.toDateString();
      "Tue Mar 15 2016"
      
      //to GMT time, ISO time
      currentDateGMT = currentDate.toGMTString();
      "Tue, 15 Mar 2016 19:01:26 GMT"
      currentDateISO = currentDate.toISOString();
      "2016-03-15T19:01:26.098Z"
      
      //to JSON
      currentDateJSON = currentDate.toJSON();
      "2016-03-15T19:01:26.098Z"
      currentDateJSON
      "2016-03-15T19:01:26.098Z"
      
      //to user friend 'locale'
      currentDateLocaleDateString = currentDate.toLocaleDateString();
      "3/15/2016"
      currentDateLocaleString = currentDate.toLocaleString();
      "3/15/2016, 2:01:26 PM"
      currentDateLocaleTime = currentDate.toLocaleTimeString()
      "2:01:26 PM"
      
      //toString
      currentDate = currentDate.toString();
      "Tue Mar 15 2016 14:01:26 GMT-0500 (Central Daylight Time)"