Every one of us faced a hard time when comes to work on Date with JavaScript. Then I thought of creating the simple utility to get familiar with JavaScript Date API
,
to find a specific date from the current date in JavaScript.
After wrote this utility, I wanted to share the same with other peers so ended up writing this article.
Date Constructor
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,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 list down 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
I created one reusable function named getDateInfo
which gives us all the information about today's date except time.
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
};
}
Utility
I shared the logic part also to give you hindsight and you will find it below.
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;
}
}
howManyDates
parameter is for how many dates from today.
type
parameter is for identifying a specific date that would fall on either past or future. Here only considered for finding the previous date value. If you're well curious enough then please go ahead do yourself for the future date.
Logic - To find the previous date
I sorted out this utility based on the below logic, You can also add if more meaningful for making reusable.
Step 1: Subtract today date from
howManyDates
countStep 2: If the subtracted value is 0 then the previous month's last date.
Step 3: If the subtracted value is > 0, then the result date of the current month
Step 4: If the subtracted value is < 0, then will derive by using the previous month and previous year as below code.
That's it. You can also share your opinion on improving further on this.
Top comments (0)