date

2021.12.22

Date in JavaScript

#JavaScript#basics
use client /components/post/reExport , date: , tags: [ ], imgUrl: , desc: , body: ( <> <H>Create</H> <p><Code>new Date([timestamp])</Code> - create a Date object with timestamp (milliseconds passed after the Jan 1st of 1970 UTC+0)</p> <Code block jsx>{ }</Code> <p><Code>new Date(dateString)</Code> - if single argument & string, then it is parsed automatically(same as Date.parse)</p> <Code block jsx>{ 2017-01-26 }</Code> <p><Code>new Date(year, month, [date, hours, minutes, seconds, ms])</Code> - create the date with the given components in the local time zone</p> <ul> <li>year = 4 digits</li> <li>month = 0 (Jan) ... 11 (Dec)</li> <li>date = day of month, if absent then 1 is assumed</li> <li>If hours/minutes/seconds/ms is absent, they are assumed to be equal 0.</li> </ul> <Code block jsx>{ }</Code> <H>All getters</H> <p><Lnk path= >JSON</Lnk> with all getter methods.</p> <Code block json>{ d = new Date() 2022-06-15T11:58:38.337Z d.getFullYear() d.getMonth() d.getDate() d.getHours() d.getMinutes() d.getSeconds() d.getMilliseconds() d.getDay() d.getTimezoneOffset() d.getTime() d.getUTCFullYear() d.getUTCMonth() d.getUTCDate() d.getUTCHours() d.getUTCMinutes() d.getUTCSeconds() d.getUTCMilliseconds() d.getUTCDay() d.toDateString() Wed Jun 15 2022 d.toISOString() 2022-06-15T11:58:38.337Z d.toJSON() 2022-06-15T11:58:38.337Z d.toGMTString() Wed, 15 Jun 2022 11:58:38 GMT d.toLocaleString() 15/06/2022, 14.58.38 d.toLocaleString( 15.06.2022 14:58:38 d.toLocaleDateString() 15/06/2022 d.toLocaleTimeString() , Wed Jun 15 2022 14:58:38 GMT+0300 (Eastern European Summer Time) d.toTimeString() 14:58:38 GMT+0300 (Eastern European Summer Time) d.toUTCString() Wed, 15 Jun 2022 11:58:38 GMT d.valueOf() 15.06.2022 14:58:38 formattedUTC 15.06.2022 11:58:38 }</Code> <H>Get</H> <p>Local time zone</p> <Code block jsx>{ }</Code> <p>UTC - counterparts methods for the time zone UTC + 0</p> <Code block jsx>{ }</Code> <H>Set</H> <p>Local time zone</p> <Code block jsx>{ July 1, 1999 }</Code> <p>Time zone UTC + 0</p> <Code block jsx>{ }</Code> <H>Local date representation</H> <p>Turkish one is the best imho.</p> <LazyImg path= /> <H>Get time in different timezone</H> <Code block jsx>{ 2022-05-09T06:03:59.000Z , { timeZone: , year: , month: , day: , hour: , minute: , hourCycle: , }) jpDate // }</Code> <H>Date correction</H> <Code block jsx>{ }</Code> <H>Time difference in ms</H> <Code block jsx>{ The loop took \${end - start} ms\ }</Code> <H>Parse</H> <ul> <li>Parses the string and returns the timestamp (number of milliseconds from 1 Jan 1970 UTC+0)</li> <li>If the format is invalid, returns NaN</li> <li>YYYY-MM-DD – year-month-day.</li> <li>The character is used as the delimiter.</li> <li>HH:mm:ss.sss – time: hours, minutes, seconds and milliseconds.</li> <li> is the time zone in the format +-hh:mm. A single letter Z would mean UTC+0.</li> </ul> <Code block jsx>{ 2012-01-26T13:51:50.417-07:00 2012-01-26T13:51:50.417-07:00 2019-01-01 2019-01-01T00:00:00.000Z 2019-01-01T00:00:00.000+00:00 Aug 9, 1995 Wed, 09 Aug 1995 00:00:00 GMT Wed, 09 Aug 1995 00:00:00 Thu, 01 Jan 1970 00:00:00 GMT-0400 }</Code> <H>performance.now()</H> <p>Gives the number of milliseconds from the start of page loading with microsecond precision</p> <Code block jsx>{ Loading started \${performance.now()}ms ago\