# const - JS | lectureNumbers, Dates, Intl & Timers

Operations with Dates

In this lecture, we will perform some operations with dates. And one cool thing that we can do with dates is to do calculations with them. For example, we can subtract one date from another to calculate how many days have passed between the two dates.

This works because whenever we attempt to convert a date to a number, the result will be the timestamp in milliseconds. And with these milliseconds, we can then perform calculations. Let me demonstrate what I just said. And for that, let's start by creating a new date.

script.js
``const date = new Date(2023, 11, 24, 10, 33, 30, 0);``

Now if we try to convert this date to a number, we will get the timestamp in milliseconds. So let's do that.

script.js
``````console.log(Number(date)); // 1703410410000

// You can also use the plus operator
console.log(+date); // 1703410410000``````

Alright! So the first operation that we will do with date will be to create a function that will calculate the number of days between two dates.

script.js
``````const calcDaysBetween = (date1, date2) => {
return date1 - date2;
};``````

For now, let's suppose that `date1` is always greater than `date2`. But in case we dont want to make this assumption, we can simply get the absolute value of the result. For now let's keep it as it is, and later I will show you what I mean.

Let's now call this function and pass in two dates.

script.js
``````const date1 = new Date(2022, 11, 28);
const date2 = new Date(2022, 11, 21);
console.log(calcDaysBetween(date1, date2)); // 604800000``````

You can see that what's returned is the number of milliseconds between the two dates. And if we want to convert this to days, we can simply divide by the number of milliseconds in a day.

script.js
``````const calcDaysBetween = (date1, date2) => {
const oneDay = 1000 * 60 * 60 * 24;
const diffInMs = date1 - date2;
return diffInMs / oneDay;
};``````

Calling this function with the same dates as before, we get the following result.

script.js
``console.log(calcDaysBetween(date1, date2)); // 7``

We get 7 days, which makes sense because from the 21st to the 28th, we have 7 days. Now, what if instead of 28 in `date1`, we had 12? Let's try that.

script.js
``````const date1 = new Date(2022, 11, 12);
const date2 = new Date(2022, 11, 21);
console.log(calcDaysBetween(date1, date2)); // -9``````

We get -9. But we don't want that. We always want it to be positive. And so, that's what I meant earlier when I said that we can simply get the absolute value of the result. Let's do that.

script.js
``````const calcDaysBetween = (date1, date2) => {
//...
return Math.abs(diffInMs / oneDay);};``````

Next, if you really need precise calculations, for example including time changes due to daylight saving changes, then you should use a date library like day.js that's available for free for all JavaScript developers.

Let's now suppose that one of the dates has the time included. For example, we have the following date.

script.js
``````const date1 = new Date(2022, 11, 12, 10, 33);
const date2 = new Date(2022, 11, 21);``````

Let's now call our function with these two dates and see what we get.

script.js
``console.log(calcDaysBetween(date1, date2)); // 8.560416666666667``

We get 8.56 days. But we don't want that. Hence, we can simply use the `Math.round()` function to round the result to the nearest integer.

script.js
``````const calcDaysBetween = (date1, date2) => {
//...
return Math.round(Math.abs(diffInMs / oneDay)n);};``````

To finish, in the next two lectures, we will then do what I mentioned earlier, which is internationalization, which is basically to format dates and numbers according to each user's locale or country.