Skip to content

date

Subtract days from date

var d = new Date("2021-01-15"); // Empty is just TODAY
console.log('Today is: ' + d.toLocaleString());
d.setDate(d.getDate() - 3);
console.log('3 days ago was: ' + d.toLocaleString());
console.log(d.toISOString())

Numbered days of the week

var dt = new Date("December 25, 1995 23:15:00");
document.write("getDay() : " + dt.getDay() );  // Sunday is 0

Number of days in a month

function daysInMonth (month, year) {
    return new Date(year, month, 0).getDate();
}

function GFG_Fun() {
    var date = new Date();
    var month = date.getMonth() + 1;
    var year = date.getFullYear();
    down.innerHTML = "Number of days in " + month
                + "th month of the year " + year 
                +" is "+ daysInMonth(month, year);
}
console.log(daysInMonth(02,2021))

Loop through dates

for (var i = 0; i < 10; i++){
    var d = new Date("2021-01-15"); 
    d.setDate(d.getDate() + i);
    console.log(d.toISOString())
}