# How to calculate days left until next Christmas using JavaScript?

In this article, you will understand how to calculate days left until next Christmas using JavaScript. The Date object works with dates and times. Date objects are created with new Date(). JavaScript will use the browser's time zone and display a date as a full text string.

## Example 1

In this example, we compute the time difference without functions.

let todayDate = new Date();
console.log("Today's date is defined as: ", todayDate)
let christmasYear = todayDate.getFullYear();
if (todayDate.getMonth() == 11 && todayDate.getDate() > 25) {
christmasYear = christmasYear + 1;
}

let christmasDate = new Date(christmasYear, 11, 25);
let dayMilliseconds = 1000 * 60 * 60 * 24;
let daysLeft = Math.ceil(
(christmasDate.getTime() - todayDate.getTime()) / (dayMilliseconds)
);
console.log("The number of days left for christmas is: ")
console.log(daysLeft)

## Explanation

• Step 1 − Define two date objects namely todayDate and christmasDate.

• Step 2 − Define a variable dayMilliseconds that holds the total milliseconds of the day.

• Step 3 − Subtract the two date values and divide the result by dayMilliseconds.

• Step 4 − Display the result.

## Example 2

In this example, we calculate the time difference using functions.

function calculateDates(christmasDate, todayDate){
let dayMilliseconds = 1000 * 60 * 60 * 24;
let daysLeft = Math.ceil(
(christmasDate.getTime() - todayDate.getTime()) /
(dayMilliseconds)
);
console.log("The number of days left for christmas is: ")
console.log(daysLeft)
}

let todayDate = new Date();
console.log("Todays date is defined as: ", todayDate)
let christmasYear = todayDate.getFullYear();
if (todayDate.getMonth() == 11 && todayDate.getDate() > 25) {
christmasYear = christmasYear + 1;
}

let christmasDate = new Date(christmasYear, 11, 25);
calculateDates(christmasDate, todayDate);

## Explanation

• Step 1 − Define two date objects namely todayDate and christmasDate.

• Step 2 − Define a variable dayMilliseconds that holds the total milliseconds of the day.

• Step 3 − Define a function calculateDates, that takes two date values as parameters.

• Step 4 − In the function, subtract the two date values and divide the result by dayMilliseconds.

• Step 5 − Display the result.

Updated on: 16-Feb-2023

426 Views