Date
Constructor
Date - built-in constructor to work with dates
new Date() - creates object of current date
new Date(year, month, date, hour, minute, second) - creates object of specific date
- month is 0-indexed
- new Date('2023-01-01') - string might be passed
- new Date(null) - returns object of Unix epoch
Static Methods
Date.now() - returns Unix timestamp
- same as getTime, but quicker
Date.parse(string) - parses date and returns Unix timestamp for that date
Date.UTC()
Methods
getTime() - returns Unix timestamp
- is helpful when dates need to be compared
getYear()
getFullYear()
similar to getYear, there are methods for getting all other date parts
setDate()
- e.g. setDate(15)
setHours() - may take hours, minutes and seconds
- returns milliseconds from Unix Epoch
setFullYear()
toLocaleDateString(locale, options) - format date to readable string
- e.g. new Date().toLocaleDateString('en-US')) - returns date of format "9/17/2016"
toLocaleString() - in addition to date it also provides time
getTimezoneOffset() - minute amount till UTC time
toISOString()
- e.g. 2024-08-29T18:12:39.944Z - output
Timezones
Coordinated Universal Time (UTC) - the primary time standard by which the world regulates clocks and time
- Greenwich Mean Time (GMT) - GMT+0 (Europe/London) is equal to UTC
Intl.DateTimeFormat().resolvedOptions().timeZone - get user timezone
Common Timezones
Central European Time (CET) - GMT+1 (Europe/Berlin)
Eastern European Time (EET) - GMT+2 (Europe/Kyiv)
- Eastern European Summer Time (EEST) - GMT+3
Pacific Time (PT) - GMT-8
Mountain Time (MT) - GMT-7
Central Time (CT) - GMT-6
Eastern Time (ET) - GMT-5 (America/New_York)
ISO 8601
International Organization for Standardization (ISO) - organization issuing international standards
ISO 8601 - defines standardized format of representing date and time
- ISO 8601 often uses UTC as the base time
YYYY-MM-DDThh:mm:ss[.mmm]Z - format of ISO 8601 date and time
- Z suffix - representation of UTC. it can be referred to as "Zulu time"
- e.g.
- 2012-06-08T16:00:00Z - UTC variant
- 2012-06-08T16:00:00+02:00 - timezone variant
Unix Time
Unix epoch - 01.01.1970, i.e. Thursday 1 January 1970 00:00:00 UT
- epoch - in computing is a date and time from which a computer measures system time. most computer systems determine time as a number of seconds
Unix time - the number of seconds that have passed since the Unix epoch. it can be called Unix timestamp
- Unix time is a system time of Unix operating systems. but it is widely used in other computer systems
- in modern computing, values are sometimes stored with higher granularity, such as microseconds or nanoseconds
- JS Date object stores and provides Unix timestamp in milliseconds