Open In App

How to calculate days left until next Christmas using JavaScript ?

Last Updated : 15 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to calculate the number of days left until next Christmas using JavaScript. Christmas marks the birth of Christ, mainly celebrated by millions of people around the globe on 25 December every year as a religious and cultural festival.

Approach: In order to calculate the number of days between two dates in JavaScript, the Date object must be used. We find out the year value of this year’s Christmas Day using getFullYear() method. We then check if the current date has already passed the Christmas day by checking if the month is December and the day is more than the 25th. The getMonth() method is used to get the month and the getDate() method is used to get the date value of the given time.

If this condition is satisfied, we add one more year to the Christmas year that we found earlier, hence advancing the Christmas Day for the next year. We then create the final date value of the next Christmas Day.

We can use the getTime() function to obtain both the date values in milliseconds. After the conversion, we subtract the later one from the earlier one to get the difference in milliseconds. The final number of days is obtained by dividing the difference (in milliseconds) between the two dates by the number of milliseconds in one day.

JavaScript Syntax:

let today = new Date();
let christmasYear = today.getFullYear();

if (today.getMonth() == 11 && today.getDate() > 25) {
  christmasYear = christmasYear + 1;
}

let christmasDate = new Date(christmasYear, 11, 25);
let dayMilliseconds = 1000 * 60 * 60 * 24;

let remainingDays = Math.ceil(
  (christmasDate.getTime() - today.getTime()) /
   (dayMilliseconds)
);

Example:

HTML




<h1 style="color: green;">
    GeeksforGeeks
</h1>
<h3>
    Program to calculate days left until
    next Christmas using JavaScript?
</h3>
<script>
    // Get the current date
    let today = new Date();
      
    // Get the year of the current date
    let christmasYear = today.getFullYear();
      
    // Check if the current date is
    // already past by checking if the month
    // is December and the current day
    // is greater than 25
    if (today.getMonth() == 11 &&
        today.getDate() > 25) {
      
      // Add an year so that the next
      // Christmas date could be used
      christmasYear = christmasYear + 1;
    }
      
    // Get the date of the next Christmas
    let christmasDate = 
        new Date(christmasYear, 11, 25);
      
    // Get the number of milliseconds in 1 day
    let dayMilliseconds =
        1000 * 60 * 60 * 24;
      
    // Get the remaining amount of days
    let remainingDays = Math.ceil(
      (christmasDate.getTime() - today.getTime()) /
      (dayMilliseconds)
    );
      
    // Write it to the page
    document.write("There are " + remainingDays +
                   " days remaining until Christmas.");
</script>


Output:



Similar Reads

How to calculate the number of days between two dates in JavaScript ?
To calculate the number of days between two dates in JavaScript, we use the Date object and perform simple arithmetic using milliseconds. Below are the methods commonly used to determine the number of days between two dates: Table of Content Using Two different datesUsing One fixed date1. Using Two different datesUse new Date() to create two date o
3 min read
How to get removed elements of a given array until the passed function returns true in JavaScript ?
The arrays in JavaScript have many methods which make many operations easy. In this article let us see different ways how to get the removed elements before the passed function returns something. Let us take a sorted array and the task is to remove all the elements less than the limiter value passed to the function, we need to print all the removed
4 min read
How to Calculate the Number of Days Between two Dates in TypeScript ?
In TypeScript, calculating the number of days between two dates is a common task in many applications. Whether you're working with date ranges, scheduling, or performing date-based calculations, knowing how to accurately determine the difference in days between two dates is essential. Different approaches to achieve this in TypeScript: Table of Con
3 min read
How to Calculate Business Days in PHP?
We have given two dates, our task is to write a PHP program to get total business days, i.e. weekdays between the two dates. Below are the approaches to calculate business days in PHP: Table of Content Using DateTime and DateIntervalUsing strtotime and date functionsUsing DateTime and DateIntervalIn this approach, we are using the DateTime class in
2 min read
How to pass variables to the next middleware using next() in Express.js ?
The following example covers how to pass variables to the next middleware using next() in Express.js. Approach: We cannot directly pass data to the next middleware, but we can send data through the request object. [Middleware 1] [Middleware 2] request.mydata = someData; -------&gt; let dataFromMiddleware1 = request.mydata; Installation of Express m
2 min read
How to show Page Loading div until the page has finished loading?
There are a lot of ways in which we can show a loading div but we have figured out the most optimal solution for you and that too in pure vanilla JavaScript. We will use the document.readyState property. When the value of this property changes, a readystatechange event fires on the document object. The document.readyState property can return these
2 min read
How to delay document.ready() method until a variable is set in jQuery ?
In this article, we will learn to delay document.ready until a variable is set. The document.ready() method is used to execute some jQuery or JavaScript scripts when the HTML DOM is completely loaded. There are two approaches that can be followed here: Approach 1: Using the holdReady() method in the jQuery library and the setTimeout() method. First
3 min read
When to use next() and return next() in Node.js ?
In this is article we will see when to use next() and return next() in NodeJS. Features: next() : It will run or execute the code after all the middleware function is finished.return next() : By using return next it will jump out the callback immediately and the code below return next() will be unreachable. Syntax: next() return next() app.use((req
2 min read
Next.js Create Next App
Next.js is an open-source JavaScript framework created by Vercel which boosts the features of React applications such as Server-Side Rendering and Static Site Generation. It provides many additional features like we get additional data fetching utilities, dynamic API routes, optimized builds, etc. React is used to create Single-Page Applications (S
3 min read
Next.js next/head
In this article, we'll learn about the Head component in NextJS and see how the head component is SEO friendly. The Head component is a built-in component provided by NextJS. We can add any element like we were adding in the head component of HTML. If you know HTML and CSS well then NextJS will be easy for you to learn. To keep everything simple th
4 min read