Nandhakumar's Display Picture

Nandhakumar

May 12, 2023

7 min read

Stop Using Date Time Libraries like Moment, Dayjs, etc …

#node.js

#date

#time

Stop Using Date Time Libraries like Moment, Dayjs, etc …

I used Moment and Dayjs library a lot for basic date calculations like adding, subtracting, and finding the difference between dates.

But, Later I found that JS itself has a good number of Date time methods to solve simple Date time use cases

Let's see what are those methods and how we can use them,

Note: For more complex DateTime calculations, it is better to use the libraries. We don't want to waste time writing complex logic from scratch.

Manipulating Date time

Hours

Adding & Subtracting Hours


const currentTime = new Date()

// currentTime
// Thu May 11 2023 06:44:51 GMT+0530 (India Standard Time)

// Adding 5 Hours to the time
currentTime.setHours(currentTime.getHours() + 5) // Thu May 11 2023 11:44:51 GMT+0530 (India Standard Time)

// Subtracting 5 Hours to the time
currentTime.setHours(currentTime.getHours() - 5) // Thu May 11 2023 06:44:51 GMT+0530 (India Standard Time)

Minutes

Adding & Subtracting Minutes


const currentTime = new Date()

// currentTime
// Thu May 11 2023 11:44:51 GMT+0530 (India Standard Time)

// Adding 5 Minutes to the time
currentTime.setHours(currentTime.getHours() + 5) // Thu May 11 2023 11:49:51 GMT+0530 (India Standard Time)

// Subtracting 5 Minutes to the time
currentTime.setHours(currentTime.getHours() - 5) // Thu May 11 2023 11:44:51 GMT+0530 (India Standard Time)

Seconds

Adding & Subtracting Seconds


const currentTime = new Date()

// currentTime
// Thu May 11 2023 11:44:51 GMT+0530 (India Standard Time)

// Adding 5 Seconds to the time
currentTime.setSeconds(currentTime.getSeconds() + 5) // Thu May 11 2023 11:49:56 GMT+0530 (India Standard Time)

// Subtracting 5 Seconds to the time
currentTime.setSeconds(currentTime.getSeconds() - 5) // Thu May 11 2023 11:44:51 GMT+0530 (India Standard Time)

You can also try manipulating year, milliseconds, timestamps etc.. Take it as an exercise and try it by yourself.

Finding Difference

Finding differences between months and years are bit tricky so, let's focus on finding the difference between days, hours, seconds, and even milliseconds.

So first we need to define the base value of days, hours, seconds, and milliseconds


const milliseconds = 1; 
const second = 1000; // 1000 milliseconds = 1 second
const minute = second * 60; // 60 seconds = 1 minute
const hour = minute * 60; // 60 minute =  1 hour
const day = hour * 24; // 24 hour = 1 day

since we have the base value now, finding the difference will be easy

  • Define two dates to find the difference,

const dateOne = new Date(2023, 5, 1);  // June 1, 2023
const dateTwo = new Date(2023, 5, 10); // June 10, 2023
  • Find the timestamp difference between the two dates


const timeStampDiff = Math.abs(dateOne.getTime() - dateTwo.getTime()); // 777600000

Why we are using Math.abs()? Math.abs() is used to ensure that the result is always a positive number, regardless of the order of the dates. It usually doesn't make sense to have a negative value when finding difference

  • Now divide the difference by the base value that we defined earlier

const difference =  timeStampDiff / day; // 9

since we divided by the day's base value we got the day difference between the two dates

which is 9, similarly you can divide timeStampDiff by other base values to get the appropriate result.

Finding Difference between months

Months

Finding the Difference between months has two possible solutions,

Solution 1 (Not the best solution)

This is straight forward solution, Get the month of two dates and get subtract to get the difference


const dateOne = new Date(2023, 5, 1);  // June 1, 2023
const dateTwo = new Date(2023, 9, 10); // Oct 10, 2023

const difference = Math.abs(dateOne.getMonth() - dateTwo.getMonth()) // 4

Even though this solution is working fine. There is an issue with this solution. If the year of the two dates is different the result(the difference) would be incorrect.

For example, for dateOne = new Date(2022, 11, 1) (December 2022) and dateTwo = new Date(2023, 0, 1) (January 2023), the result would be Math.abs(0 - 11) = 11, which is incorrect as the difference is actually 1 month.

Solution 2

This is a bit tricky, But I'll explain to you step by step

  • Let’s define the date first

const dateOne = new Date(2022, 5, 1);  // June 1, 2022
const dateTwo = new Date(2023, 9, 10); // Oct 10, 2023

Note: In Js Dates, months start from 0 i.e (Jan → 0, Feb → 1)

  • Find the year difference

const yearDifference = dateTwo.getFullYear() - dateOne.getFullYear(); // 1
  • Find the month difference

const monthDifference = dateTwo.getMonth() - dateOne.getMonth(); // 4
  • Calculate total no. of months of the yearDifference

let months = yearDifference * 12 // 12
  • Now add the monthDifference to months

months += monthDifference; // 16

That’s it we have found the difference.

Also you can add Math.abs(months) as one other step, so you can avoid getting negative values

Finding Difference between years

This also a bit tricky, but not like months

  • Let’s First Define the dates

const dateOne = new Date(2022, 5, 1);  // June 1, 2022
const dateTwo = new Date(2023, 9, 10); // Oct 10, 2023
  • Find the difference in year

let difference = dateTwo.getFullYear() - dateOne.getFullYear();
  • You might think just by subtracting two year can get the expected result. But, there are few scenarios to be handled. Example: If you have dates like Jan 1 2023 and Dec 31 2023, here the year is same but the diff is 1 year. and in another case, if the dates are Dec 31 2023 and Jan 1 2024, here the year is different but the actual difference is 1 day.

You get the point right?

So to handle this, you can add few conditions


if (dateTwo.getMonth() < dateOne.getMonth() || 
   (dateTwo.getMonth() == dateOne.getMonth() && 
    dateTwo.getDate() < dateOne.getDate())) {
    difference--;
}

difference = Math.abs(difference)

Let’s break down the conditions

  • dateTwo.getMonth() < dateOne.getMonth() - This means that in the current year of dateTwo, dateTwo has not yet reached the month of dateOne. For example, if dateOne is June 10, 2023 and dateTwo is March 15, 2024, even though dateTwo's year is 2024, it hasn't yet reached the month of June, so we should consider the year difference as 0,
    not 1.
  • (date2.getMonth() == date1.getMonth() && date2.getDate() < date1.getDate()) - This means that even though dateTwo is in the same month as dateOne, it hasn't yet reached the day of dateOne in its current year. For example, if dateOne is June 10, 2023 and dateTwo is June 5, 2024, even though dateTwo's year is 2024 and the month is June, it hasn't yet reached the 10th of June, so we should consider the year difference as 0, not 1.

In both of these conditions, we decrease difference by one (difference**--**) to accurately reflect the year difference considering the month and day, not just the year.

Finally we use Math.abs() as we don’t want negative values

Conclusion

In this post you learned how to manipulate date and time yourself without using libraries. However the Date time libraries are always a time saver. When you have more complex use case to solve that is based on Date time, You can use these libraries.

If you are going to perform only simple calculation utilise existing Date time module in node.js.

Again this is my thought, based on your requirement you can decide whether to use libraries or not.


Thanks For Reading!

Hope you have learned something new today 😊.

I welcome your questions, feedback, and discussions on this topic. Don't hesitate to reach out if there's something you'd like to talk about.

If you find this post helpful Tweet this Post

Follow and connect with me on Twitter, Instagram, Email and LinkedIn for more interesting stuff like this.

Cheers ✌️