JavaScript:
Calculating a date in the future or past
How to:
JavaScript’s Date
object is your go-to for date gymnastics. Let’s play with some examples:
// Today's date
let today = new Date();
console.log(today); // Outputs current date and time
// Calculate a date 7 days in the future
let nextWeek = new Date();
nextWeek.setDate(today.getDate() + 7);
console.log(nextWeek); // Outputs date for the same time, 7 days ahead
// Calculate a date 30 days in the past
let lastMonth = new Date();
lastMonth.setDate(today.getDate() - 30);
console.log(lastMonth); // Outputs date for the same time, 30 days ago
// Set a date 1 year in the future
let nextYear = new Date();
nextYear.setFullYear(today.getFullYear() + 1);
console.log(nextYear); // Outputs date for the same time next year
Outputs depend on when you run this code, as today
is your current date-time.
Deep Dive
Before JavaScript had built-in date-manipulation functions, programmers had to manually calculate dates, accounting for variations in month lengths, leap years, and time zones—a real pain! With Date
, much of this hassle goes away.
Alternatives to the native Date
object include libraries like moment.js
and date-fns
, which offer richer syntax and solve quirks like daylight saving time bugs.
When calculating dates, remember: Date
counts months from 0 (January) to 11 (December), not 1-12. And don’t forget leap years when working with February dates.
See Also
- MDN Web Docs on Date: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
- moment.js: https://momentjs.com/
- date-fns: https://date-fns.org/