Last active
March 15, 2016 20:19
-
-
Save csdear/59bcc817c9afe1967d27 to your computer and use it in GitHub Desktop.
Javascript DATE and TIME functions : Creation and Conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment