Last updated on March 2, 2020
Every one of us faces a hard time when comes to work on Date
with JavaScript. So thought of creating this simple kind of utility
to find a specific date from today’s date in JavaScript.
Looking for code snippet for this article?
Disclaimer: Only Local Time Zones are considered for this article.
Introduction
To get the current date we have a Date API from JavaScript, new Date();
The same can be retrieved by passing date string as an input to the new Date();
As most of us haven’t exposed to one more way of getting date value just by constructing the parameters like this,new Date(year, month, date, hh, mm, ss);
Find a specific date from today’s date in JavaScript
To solve any kind of problem first, we need to collect the required inputs and draw the pseudo-code if required.
In short, we require the following inputs
- Logic to derive the previous date,
- Day count of the previous month,
- Previous Year
In order to achieve this JavaScript Challenge, I have created one reusable function that gives us all the information about today date except time. [getDateInfo] which returns the object holding all date information.
function getDateInfo() {
// Date Calculation
const todayDate = new Date();
const months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
const dayCount = {
'January': 31,
'February': (todayDate.getFullYear()%4 === 0) ? 29 : 28,
'March': 31,'April': 30,'May':31,'June':30,'July':31,'August':31,'September':30,'October':31,'November':30,'December':31
}
const days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
const currentMonth = months[todayDate.getMonth()];
const currentYear = todayDate.getFullYear();
const currentDate = todayDate.getDate();
const currentDay = days[todayDate.getDay()];
return {
todayDate, months, dayCount, days, currentMonth, currentYear, currentDate, currentDay
};
}
Finally, we came to the main logic area where we will have to find a specific date from today’s date. The logic is as below in steps,
- Step 1: Subtract today date from input date count (Here howManyDates is an input)
- Step 2: If subtracted value is
0
then previous month last date. - Step 3: If subtracted value is
> 0
, then the result date of the current month - Step 4: If subtracted value is
< 0
, then will derive by using theprevious month and previous year
as below code.
function findDateFromToday({ howManyDates = 0, type = 'past' }){
try {
const { todayDate, months, dayCount, days, currentMonth, currentYear, currentDate, currentDay} = getDateInfo();
if(type === 'past') {
const resultDate = currentDate - howManyDates;
if(resultDate === 0) {
// Starting date of the current month
return new Date(currentYear, todayDate.getMonth(),0,0,0);
} else if(resultDate > 0) {
// resultDate of the month
return new Date(currentYear, todayDate.getMonth(), resultDate, 0,0,0);
} else {
let prevMonth;
let prevYear;
// if negative, then (previous month day count - (negative resultDate))
if(todayDate.getMonth() !== 0) {
prevMonth = todayDate.getMonth() - 1;
const prevDate = dayCount[months[prevMonth]];
return new Date(currentYear, prevMonth, prevDate +resultDate,0,0,0);
} else {
// previous year
prevYear = currentYear - 1;
// previous month
prevMonth = 11; // december
const prevDate = dayCount[months[prevMonth]];
return new Date(prevYear, prevMonth, prevDate + (resultDate),0,0,0);
}
}
}
} catch(error) {
return error;
}
}
Why we havetype
parameter here is that I planned it to extend for the future date finding also as previous date finding.
Want to know more about the possibilities of JavaScript Date API, then you definitely check out author Zell’s article.