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());
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

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

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()
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