Why we need to konw date?
- We need to handle date time-zone influence during international project.
- We need to konw date format showing in client.
How can we learn it?
- Jave Script Document
- ES5/6 Document
- Test in browsers
What is the solution/result?
- UTC (Universal Time Coordinated) is the same as GMT (Greenwich Mean Time).
- Omitting T or Z in a date-time string can give different results in different browsers.
- (TZ)When setting a date, without specifying the time zone, JavaScript will use the browser’s time zone.
- (TZ)When getting a date, without specifying the time zone, the result is converted to the browser’s time zone.
- (TZ)In other words: If a date/time is created in GMT (Greenwich Mean Time), the date/time will be converted to CDT (Central US Daylight Time) if a user browses from central US.
- In some browsers, months or days with no leading zeroes may produce an error:
const d = new Date("2015-3-25");
- The behavior of “YYYY/MM/DD” is undefined.
Some browsers will try to guess the format. Some will return NaN.
const d = new Date("2015/03/25")
- The behavior of “DD-MM-YYYY” is also undefined.
Some browsers will try to guess the format. Some will return NaN.
const d = new Date("25-03-2015");
- UTC
const d = (new Date()).toUTCString(); // 'Wed, 09 Mar 2022 07:18:02 GMT'
- ISO
const d = (new Date()).toISOString(); // '2022-03-09T07:18:59.078Z'
- Local
const d = (new Date()).toLocaleString(); // '2022/3/9 13:58:15'
https://www.w3schools.com/js/js_date_formats.asp
https://www.w3schools.com/js/js_es6.asp