Categories
Featured Development Resources HTML/Javascript

Javascript Date Operations – Subtract, Add, And Sort

There are a number of date library’s in JavaScript, but for the most part they are unnecessary and not needed for most operations.

In this article we’ll cover a number some of the most common date operations.

Subtracting Dates

In order to subtract two dates in JavaScript you will first need to create a date object and using the getTime() function you can use the minus operator to subtract the dates with the result being returned in milliseconds.

var date1 = new Date("December 7, 2022")
var date2 = new Date("December 1, 2022")
console.log(date2.getTime()-date1.getTime())
// -518400000

If you’d like to get the number of dates between two dates you can use the getDate() function instead of getTime().

var date1 = new Date("December 7, 2022")
var date2 = new Date("December 1, 2022")
console.log(date2.getDate()-date1.getDate())
// -6

Subtracting Days From A Date

Subtracting days from a date is similar to subtracting one date from another in that you need to first convert the date to a date object, then use the minus operator on dates with the getDate() function applied with the number of days you would like to subtract.

You can use the toLocaleString() function to set back to a readable date format.

// set to the current date
let d = new Date();
// set date to 7 days ago
d.setDate(d.getDate() - 7);
console.log(d.toLocaleString());
// displays the date 7 days ago

Adding Days From A Date

Adding days is done by converting the dates into a date object and then using the + operator.

// set to the current date
let d = new Date();
// set date to 7 days ago
d.setDate(d.getDate() + 7);
console.log(d.toLocaleString());
// displays the date 7 days from now

Sorting Dates

Sorting an array of dates in ascending order  can be done using the internal sort method after the dates have been converted to Date objects.

// an array of dates
const datesArr = [
    new Date("12-7-2022"),
    new Date("1-1-1980"),
    new Date("5-5-1990"),
    new Date("11-11-2010"),
];

// sort the dates in descending order
datesArr.sort((a,b)=>a.getTime()-b.getTime())

console.log(datesArr)
// [Tue Jan 01 1980 00:00:00 GMT-0800 (Pacific Standard Time), 
// Sat May 05 1990 00:00:00 GMT-0700 (Pacific Daylight Time), 
// Thu Nov 11 2010 00:00:00 GMT-0800 (Pacific Standard Time), 
// Wed Dec 07 2022 00:00:00 GMT-0800 (Pacific Standard Time)]

Sorting in descending order requires a sort function to be passed to sort as shown here:

// an array of dates
const datesArr = [
    new Date("12-7-2022"),
    new Date("1-1-1980"),
    new Date("5-5-1990"),
    new Date("11-11-2010"),
];

// sort the dates in descending order
datesArr.sort((a,b)=>b.getTime()-a.getTime())

console.log(datesArr)
// [Wed Dec 07 2022 00:00:00 GMT-0800 (Pacific Standard Time), 
// Tue Jan 01 1980 00:00:00 GMT-0800 (Pacific Standard Time), 
// Sat May 05 1990 00:00:00 GMT-0700 (Pacific Daylight Time), 
// Thu Nov 11 2010 00:00:00 GMT-0800 (Pacific Standard Time)]

Conclusion

Performing date operations in JavaScript is not that complicated once you understand that you need to use the getDate() and getTime() functions to perform operations on those dates.