Javascript - Date
Table of Contents
About
A date object to store date information.
- the getMonth function is 0 based. Therefore January is at 0 and not 1
- the getYear function is deprecated for getFullYear - due to a year 2000 problem, this function does not return a valid year number.
Example
now = new Date();
console.log("We are the "+now.getDate()+"-"+(now.getMonth()+1)+"-"+now.getFullYear());
console.log("We are in String the "+now.toString());
console.log("We are in Date String the "+now.toDateString());
console.log("We are in ISO String the "+now.toISOString());
Management
Init
Current time
now = new Date();
Date.now
Date.now() returns the number of milliseconds in epoch scale and not a date object. It's equivalent to new Date().getTime().
Proof
console.log(Date.now());
console.log(Date.now() == new Date().getTime());
console.log((new Date(Date.now())).toString());
From Calendar Numbers
- Date level
let date = new Date(2018,3,30);
const time = { "year": date.getFullYear(), "month": date.getMonth(), "day": date.getDate(), "hours": date.getHours(), "minutes": date.getMinutes(), "seconds": date.getSeconds() };
console.log(time);
- At the second level
new Date(1973, 07, 20, 8, 30, 0)
From epoch
With an epoch timestamp
let epoch = Date.now();
console.log("The current epoch is "+epoch);
let date = new Date(epoch);
const time = { "year": date.getFullYear(), "month": date.getMonth(), "day": date.getDate(), "hours": date.getHours(), "minutes": date.getMinutes(), "seconds": date.getSeconds() };
console.log(time);
From String
- Passing a string in the constructor. For instance with an ISO8601 date string
let date = new Date('2022-02-18T09:34:32.255Z');
const time = { "year": date.getFullYear(), "month": date.getMonth(), "day": date.getDate(), "hours": date.getHours(), "minutes": date.getMinutes(), "seconds": date.getSeconds() };
console.log(time);
- With the parse 1) function, you get back a epoch timestamp number
let epoch = Date.parse('2022-02-18T09:34:32.255Z');
console.log(epoch);
let date = new Date(epoch );
const time = { "year": date.getFullYear(), "month": date.getMonth(), "day": date.getDate(), "hours": date.getHours(), "minutes": date.getMinutes(), "seconds": date.getSeconds() };
console.log(time);
ToString
To Iso8601 String
To Date String
let date= new Date();
console.log(date.toDateString());
To Only Time Part (Hour, Min, Sec)
let date= new Date();
console.log(date.toLocaleTimeString()); // without zone
console.log(date.toTimeString()); // with zone
To Custom String such as SQL Timestamp String
Custom: There is already a lot of toString function but none of them give the ability to pass a string format.
For example, the SQL timestamp string
- the YYYY-MM-DD date level format (with pad function)
date = new Date(2018,3,1);
dateString = `${date.getFullYear()}-${(date.getMonth()+1).toString().padStart(2,'0')}-${date.getDate().toString().padStart(2,'0')}`;
console.log(dateString);
- the YYYY-MM-DD HH:MM:SS format with pad function.
date = new Date();
dateString = `${date.getFullYear()}-${(date.getMonth()+1).toString().padStart(2,'0')}-${date.getDate().toString().padStart(2,'0')} ${date.getHours().toString().padStart(2,'0')}:${date.getMinutes().toString().padStart(2,'0')}:${date.getSeconds().toString().padStart(2,'0')}`;
console.log(dateString);
Add / Subtstract
var today = new Date();
console.log("Today: "+today);
var tomorrow = new Date(today.getTime()+1000*60*60*24);
console.log("Tomorrow: "+tomorrow);
Compare
You can't compare date directly, you need to transform it to the time (a number that is the number of ms since epoch) and compare it.
Example:
var today = new Date();
var expirationDate = new Date(today.getTime() - 30);
if (expirationDate.getTime() < today.getTime()) {
console.log("The date has expired");
}
TimeZone
Hight Resolution Time
The following script may record a positive number, negative number, or zero for computed duration: High Resolution API tries to correct this behavior 2)
var mark_start = Date.now();
doTask(); // Some task
var duration = Date.now() - mark_start;
Library
- https://momentjs.com/ - Parse, validate, manipulate, and display dates and times in JavaScript.