Get the Day of the Year in JavaScript

Sometimes it can be handy to find the day of the year* that a particular date falls on in JavaScript.

Let’s say you want to get the day of the year for Pie Day (14 Mar 2024). Check out the code:

*This tutorial is for the Gregorian Calendar.

The Code

The result would be:

Date: Thu Sep 05 2024 12:01:14 GMT+0700 (Indochina Time)
Year Day: 74

Try the same for the current date with: new Date()

How does it work?

Our goal here is to convert the current date into a time in milliseconds and then subtract that time from the last day of the previous year before converting those milliseconds to a number of days.

We could get a little stuck with daylight saving time changes, so it’s a good idea to convert our date to Coordinated Universal Time (UTC) using the JavaSript UCT() method. This will also conveniently store our current time in milliseconds.

Line 6 – Here we create a function called getYearDay that has a parameter, date.

Lines 7-11 – First we store our current date in UTC millisecond formation. We can apply the year, month, day of month formatting as arguments here.

Line  13-17 – Next, we need to get the last day of the previous year. We can cheat by setting the day of the month to zero. This will indicate the first day of the month minus 1. That is the last day of the previous month/year.

Line 19 – Now we need to get a full day in milliseconds to divide our end result by it to get the number of days.

Line 21 – Finally, we subtract our target UTC date by the last day of the previous year in UTC and then divide that by our day in milliseconds. This is then returned from the function.

Hire a Google Workspace Developer for your Business Needs

The Video Tutorial

The Video

 

Create a JavaScript Date prototype for Get Year Day

If you want a more chainable function that can become one with your Date class then we can convert the getYearDay() function to a prototype method.

Check it out:

The  Video Tutorial

The Video Tutorial

Released 14 Sep 2024

Get the Day of the Year of the first Day of Each month in JavaScript

In this example,  we will generate a list of days of the year for the start of each month.

Add the following code below the getYearDay() function above.

🐐You can support me for free by using this Amazon affiliate link in your next tech purchase :Computers & Stuff! 🐐

Running this code you should see the following results:

This function has an optional parameter. If you leave it blank you will get the current year, but you can add in a specific year.

In this function, we iterate over the 12 months of the year (keeping in mind that JavaScript dates start at zero) and create a new date for the start of each month.

Then we send our date to the getYearDay() function to retrieve the day of the year.

Finally, we store that day in our array of days of the year and return it.

Get an Array of Days of the Year for a Particular Day of the Week throughout the year

What days of the year does each Monday fall on in 2024?

Calendar for January 2024

We can see from the image above that Monday would start its count from 1, 8, 15, 22, 29, 34 … etc.

What about Monday in 2025?

January 2025 calendar

Here the array would start like this:

6, 13, 20, 27, 30, 37, ...

How would we calculate this in JavaScript?

The Code

Go ahead and run the code. You should get the following logged arrays:

Arguments

We’re a little more helpful in this function for the user arguments:

  1. weekdayNumber or String: Users can provide a zero-based number of the week starting on Sunday (0) and ending on Monday (6) – USA-style. Or they can add a day of the week and we will interpret it.
  2. year– Number (optional): A 4-digit number representing a year or the user can leave it blank to get the current year.

Line 8 – Uses the same trick as our previous function checking if the date is false for the current year or just provide the user’s date.

Lines 12-19 – We also need to check if the weekday is a number or if the day of the week is a word (string). If it is a string, we trim out the whitespace, make it all lowercase and just get the first two letters. We then compare those letters against our date of the week array, returning the index of the corresponding position.

Finally, we add one to this to move it from a zero-based day.

First and Last day of the year.

Next, we need to find which weekday the first day of the year falls on.

Line 22 – We can do this by getting the JavaScript Date of the first day of the selected year and calling the getDay() method to retrieve the day of the week and adding one.

Create and Publish Google Workspace Add-ons with Apps Script Course 300px

Line 24 – We also need to get the last day of the year. This is either going to be 365 or 366, depending on if the year is a leap year. There is a faster way of doing this but while we are using getYearDay in this tutorial, we may as well use it again here.

Is a Year a Leap Year? Gregorian Calendar & JavaScript

Iterate Over the Days to calculate the days.

Lines 26 – 33 – Now we can iterate over our year range, incrementing by 7 days at a time.

Line 28 –We set our day by subtracting the incrementor (i) from the first day of the year and adding the target weekday plus one.

Line 29 – Should the resulting day be a negative number, then we will not report it.

Line 30 – If the resulting day is greater than the total number of days of the year, then we will break our loop. We’re done here.

Line 32 – We then push the day to our list of days.

Finally, we return the days back from our function.

 

Leave a Reply