Date
is a built-in object which stores the date, time and provides methods for date/time management.
let now = new Date();
console.log(now); // shows current date/time
// The values passed to the object create a Date object with the time equal to number of milliseconds.
// 1/1000 of a second, passed after the Jan 1st of 1970 UTC+0. 0 means 01.01.1970 UTC+0
// The number of milliseconds that has passed since the beginning of 1970 is called a timestamp.
let Jan01_1970 = new Date(0);
// now add 24 hours, get 02.01.1970 UTC+0
let Jan02_1970 = new Date(24 * 3600 * 1000);
// We can convert the existing Date object to a timestamp using the date.getTime() method.
// It returns the timestamp for the date – a number of milliseconds passed from the January 1st of 1970 UTC+0.
console.log(now.getTime());
// The setTime() method sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC.
var event = new Date('July 1, 1999');
now.setTime(event.getTime());
// Date.now() returns the current timestamp.
// It is semantically equivalent to new Date().getTime()
// However, it doesn’t create an intermediate Date object. So it’s faster and doesn’t put pressure on garbage collection.
let nowTimeSpan = Date.now(); // milliseconds count from 1 Jan 1970
// getTimezoneOffset() returns the difference between the local time zone and UTC, in minutes:
// if you are in timezone UTC-1, outputs 60
// if you are in timezone UTC+3, outputs -180
console.log(new Date().getTimezoneOffset());
- The time portion of the date is assumed to be midnight GMT and// is adjusted according to the timezone the code is run in.
- So the result could be Thu Jan 26 2017 11:00:00 GMT+1100 (Australian Eastern Daylight Time) or Wed Jan 25 2017 16:00:00 GMT-0800 (Pacific Standard Time).
- You can create the date with the given components in the local time zone using
new Date(year, month, date, hours, minutes, seconds, ms)
.
- Only two first arguments are obligatory.
- The minimal precision is 1 ms (1/1000 sec).
new Date(2011, 0, 1, 0, 0, 0, 0); // // 1 Jan 2011, 00:00:00
new Date(2011, 0, 1); // the same, hours etc are 0 by default
new Date(2011, 0, 1, 2, 3, 4, 567); // 1.01.2011, 02:03:04.567
Access Date Components
- All the following methods return the components relative to the local time zone.
- There are also their UTC-counterparts, that return day, month, year and so on for the time zone UTC+0.
var moonLanding = new Date('July 20, 69 01:19:18');
console.log(moonLanding.getFullYear()); // 1969
console.log(moonLanding.getMonth()); // 6 (January gives 0)
console.log(moonLanding.getDate()); // 20 - Get the day of month, from 1 to 31.
console.log(moonLanding.getHours()); // 1
console.log(moonLanding.getMinutes()); // 19
console.log(moonLanding.getSeconds()); // 18
console.log(moonLanding.getMilliseconds()); // 0
console.log(moonLanding.getDay()); // 0 - Sunday - Saturday : 0 - 6
console.log(moonLanding.getUTCFullYear());
console.log(moonLanding.getUTCMonth());
console.log(moonLanding.getUTCDate());
console.log(moonLanding.getUTCHours());
console.log(moonLanding.getUTCMinutes());
console.log(moonLanding.getUTCSeconds());
console.log(moonLanding.getUTCMilliseconds());
console.log(moonLanding.getUTCDay());
Setting Date Components
var event = new Date('August 19, 1975 23:15:30');
event.setFullYear(2000);
event.setMonth(3);
event.setDate(24);
event.setHours(20);
event.setMinutes(45);
event.setSeconds(42);
event.setMilliseconds(456);
Auto-Correction
- With this feature we can set out-of-range values, and it will auto-adjust itself.
let date = new Date(2013, 0, 32); // 1st Feb 2013
date.setSeconds(date.getSeconds() + 70);
date.setDate(0); // Min day is 1, so the last day of the previous month is assumed
Date to Number and Date Diff
let date = new Date();
alert(+date); // the number of milliseconds, same as date.getTime()
- Dates can be subtracted and the result is their difference in ms.
let start = new Date(); // start counting
for (let i = 0; i < 100000; i++) {
let doSomething = i * i * i;
}
let end = new Date(); // done
alert( `The loop took ${end - start} ms` );
Date.parse