xxxxxxxxxx
const d1 = new Date("Mon Jan 3 2022 16:38:58 GMT+0530 (India Standard Time)")
const d2 = new Date("2022-01-03T18:30:00Z")
console.log(d1)
console.log(d2)
if (d1 < d2) console.log("Date 1 is earlier than d2")
// To find hh:mm:ss difference for the same day we can do this.
console.log(new Date(d2-d1).toISOString().substr(11, 8))
xxxxxxxxxx
const date1 = '25/02/1985';
const date2 = '26/02/1985';
// input 28/10/2018
// input DD/MM/YYYY
export const convertToDate = (dateSting) => {
const [day, month, year] = dateSting.split("/");
return new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
}
console.log(convertToDate(date1) > convertToDate(date2))
xxxxxxxxxx
let myDate = new Date("January 13, 2021 12:00:00");
let yourDate = new Date("January 13, 2021 15:00:00");
if (myDate < yourDate) {
console.log("myDate is less than yourDate"); // will be printed
}
if (myDate > yourDate) {
console.log("myDate is greater than yourDate");
}
xxxxxxxxxx
let d1 = new Date();
let d2 = new Date();
// can use >, <, <=, <=
d1 > d2
d1 >= d2
// == won't work so can use this:
(d1 >= d2) && (d2 >= d1)
xxxxxxxxxx
// Takes two strings as input, format is dd/mm/yyyy
// returns true if d1 is smaller than or equal to d2
function compareDates(d1, d2){
var parts =d1.split('/');
var d1 = Number(parts[2] + parts[1] + parts[0]);
parts = d2.split('/');
var d2 = Number(parts[2] + parts[1] + parts[0]);
return d1 <= d2;
}
xxxxxxxxxx
const d = new Date();
const cd = new Date('2023-5-18');
if(d.getDate() === cd.getDate() && d.getMonth() === cd.getMonth() && d.getFullYear() === cd.getFullYear()) {
console.log(true);
} else {
console.log(false);
}