Let's meet a new integrated object: new Date().
It saves the date, time and gives data/time management options.
We may, for instance, utilize it for saving creation / change timings, measuring time or just printing the actual date.
Call new Date()
with one of the following parameters to generate a new Date()
object:
Creating an object for the current new Date()
Date
and time without any argument:
let now = new Date(); // shows current date/time
// alert! change runkit node version to v16
Create a A time stamp is termed an integer count which represents the amount of milliseconds that transpired since early 1970. There are negative timestamps before 1 January 1970, e.g.:new Date(milliseconds)
Date
object with the time equivalent to the milliseconds after 1 Jan 1970 UTC+0. (1s / 1000ms)
// 0 means 01.01.1970 UTC+0
let Jan01_1970 = new Date(0);
// alert! change runkit node version to v16
This is a numerically lightweight representation of a Date
. With a timestamp, we can always generate a date
using a new Date()
and convert the old database object to a timestamp.
The method .getTime()
(see below).
// now add 24 hours, get 02.01.1970 UTC+0
let Jan02_1970 = new Date(24 * 3600 * 1000);
// alert! change runkit node version to v16
// 31 Dec 1969
let Dec31_1969 = new Date(-24 * 3600 * 1000);
// alert! change runkit node version to v16
If a single parameter is present and it is a string, it will be automatically scanned. The time is not provided and is presumed to be GMT by midnight and adjusted to the time zone in which the code will work, thus the outcome may be new Date("datestring")
We're going to cover the algorithm like Date.parse
does later.
let date = new Date("2017-01-26");
// alert! change runkit node version to v16
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)
In the local time zone, create the date with the components. Only the first two are require. For instance: The maximal precision is 1 ms (1/1000 sec):new Date(year, month, date, hours, minutes, seconds, ms)
year
needs four numbers: 2013
is all right, 98
isn't.month
begins with 0
(Jan), to 11
(Dec).date
argument was not present, the date argument is 1
.hours/minutes/seconds/ms
are missing, 0
is assumed.
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
// alert! change runkit node version to v16
let date = new Date(2011, 0, 1, 2, 3, 4, 567); // 1.01.2011, 02:03:04.567
// alert! change runkit node version to v16
Thanks for reading the article ❤️
Top comments (0)