Get the day of the year (1 – 366) in JavaScript

Get the Day of the Year (1 - 366) in JavaScript

We will learn how to get the day of the year (1 – 366) in JavaScript using two different methods, namely new Date() and dayOfYear(). Each method is rather straightforward, and you can choose whichever you want.

Get the day of the year (1 – 366) in JavaScript

Using new Date()

The new Date() constructor creates a date from a string that represents the date and then returns a Date object. If no parameter is passed to the function, it will return a Date representing the current day. However, you can specify the year, month, and day through this method.

Syntax:

new Date(year, monthIndex, day)

Parameter:

  • year: A number represents the desired year of the date
  • monthIndex: A number represents the desired month starting from 0th for January
  • day: A number represents the desired day of the date

To get the day of the year, you need to get the first day of that year, for example, 2022, by specifying the year is 2022 only:

// Get the first day of a year 2022
let firstDay = new Date(2022, 0);
console.log(firstDay);

Output:

Sat Jan 01 2022 00:00:00 GMT+0700 

After you get the first day of the year, you can compare it with your date object in which you want to get the day of the year (1-366):

// Get the first day of a year
let firstDay = new Date(2022, 0);

// Get the current day
let today = new Date();

// Get the day of the year
let dayOfYear = Math.floor((today - firstDay) / 1000 / 60 / 60 / 24) + 1;
console.log(dayOfYear);

Output: 

358

As you can see, the examples above have produced the expected result. However, if you don’t know exactly what the current year is as it is not a constant. Then you can use the getFullYear() method in order to get the current year and get the first year from it:

// Get the current day
let today = new Date();

// Get the first day of a year
let firstDay = new Date(today.getFullYear(),0);

// Get the day of the year
let dayOfYear =  Math.floor((today - firstDay) / 1000 / 60 / 60 / 24) + 1;
console.log(dayOfYear);

Output: 

358

Using dayOfYear()

This dayOfYear() method comes from the moment.js library, which needs importing from the src tag:

<script src = "https://momentjs.com/downloads/moment.js"> </script>

You can create a date using moment() and get the day of the year using dayOfYear():

<html>
  <body>
    <h1>LearnShareIT</h1>
    <script src="https://momentjs.com/downloads/moment.js"></script>
    <script type="text/javascript">
      let today = moment();
      // Get the day of the year of a moment Object
      alert(today.dayOfYear());
    </script>
  </body>
</html>

Output:

357

If you already have a date object which is not the current day and you want to get the day of the year using that date object, then you just need to pass that Date object into the moment constructor to convert it to a moment object. Then you can call the dayOfYear() method:

<html>
  <body>
    <h1>LearnShareIT</h1>
    <script src="https://momentjs.com/downloads/moment.js"></script>
    <script type="text/javascript">
      let myDate = new Date("31 Dec 2022");
      let momentDay = moment(myDate);
      // Get the day of the year of a moment Object
      alert(momentDay.dayOfYear());
    </script>
  </body>
</html>

Output: 

365

As you can see, this method not only produced the expected result but also is easy to use as the others.

Summary

We have learned to get the day of the year (1 – 366) in JavaScript. If you don’t want to perform mathematical operations but you want simpler methods, you should consider using the second solution (using dayOfYear() method). Otherwise, if you want to use built-in functions, use the first solution.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *