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.
Table of Contents
The Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/** * Finds the day of the year for the selected date. * @param {Date} date - A JavaScript Date. * @returns {Number} day of the year. */ function getYearDay(date){ const dateUTC = Date.UTC( date.getFullYear(), date.getMonth(), date.getDate() ) const dateYearStartUTC = Date.UTC( date.getFullYear(), 0, 0 // Gets the 31st of the previous year. ) const dayMilliseconds = 24 * 60 * 60 * 1000 return (dateUTC - dateYearStartUTC) / dayMilliseconds } const date = new Date(2024, 2, 14) console.log("Date: ", date) console.log("Year Day: ", getYearDay(date)) |
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.
The Video Tutorial
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/** * Finds the day of the year for the selected date.. * @memberof Date * @returns {Number} day of the year. */ Date.prototype.getYearDay = function(){ const dateUTC = Date.UTC( this.getFullYear(), this.getMonth(), this.getDate() ) const dateYearStartUTC = Date.UTC( this.getFullYear(), 0, 0 // Gets the 31st of the previous year. ) const dayMilliseconds = 24 * 60 * 60 * 1000 return (dateUTC - dateYearStartUTC) / dayMilliseconds } const testDatePrototype = new Date(2024, 0, 31) console.log(testDatePrototype.getYearDay()) |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/** * Gets the day of the year for for the start of each month. * @param {number} [year] - The target year * @returns {Number[]} */ function getFirstOfMonthAsYearDay(year = false){ if(!year){ year = new Date().getFullYear() } let firstInDaysOfYear = [] for( let i = 0; i < 12; i ++){ const dayOfYear = getYearDay(new Date(year, i, 1)) firstInDaysOfYear.push(dayOfYear) } return firstInDaysOfYear } console.log("Year: current") console.log("Array: ", getFirstOfMonthAsYearDay()) console.log("Year: 2000") // Leap year console.log("Array: ", getFirstOfMonthAsYearDay(2000)) console.log("Year: 2025") // Not a leap year console.log("Array: ", getFirstOfMonthAsYearDay(2025)) |
Running this code you should see the following results:
1 2 3 4 5 6 7 8 9 10 |
Year: current // 2024 Array: (12) [1, 32, 61, 92, 122, 153, 183, 214, 245, 275, 306, 336] Year: 2000 Array: (12) [1, 32, 61, 92, 122, 153, 183, 214, 245, 275, 306, 336] Year: 2025 Array: (12) [1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335] |
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?
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?
Here the array would start like this:
6, 13, 20, 27, 30, 37, ...
How would we calculate this in JavaScript?
The Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
/** * Get the weekday year day array * @param {Number | String} weekday - Zero-based day of the year OR Day of the week. * @param {Number} [year] - 4 digit number. * @returns {Number} */ function weekdayYearDayArray(weekday, year = false){ if(!year){ year = new Date().getFullYear() } // Check weekday type and convert if string if(typeof weekday === "string"){ weekday = weekday.trim().toLocaleLowerCase().substring(0,2) const daysOfWeek = ["su","mo","tu","we","th","fr","sa"] weekday = daysOfWeek.findIndex(d => d == weekday) } weekday++ // Get the first day of the year as weekday const firstDayOfYr = new Date(year, 0, 1).getDay() + 1 const totalDays = getYearDay(new Date(year + 1, 0, 0)) const daysOfYear = [] for(let i = 0; i <= totalDays; i+=7){ const day = i - firstDayOfYr + weekday + 1 if(day < 1) continue if(day > totalDays) break daysOfYear.push(day) } return daysOfYear } console.log("\n#### 2024 ####") console.log("Monday\n", weekdayYearDayArray("Mon", 2024)) console.log("Tuesday\n", weekdayYearDayArray("Tue", 2024)) console.log("Wednesday\n",weekdayYearDayArray("Wed", 2024)) console.log("Thursday\n",weekdayYearDayArray("Thu", 2024)) console.log("Friday\n",weekdayYearDayArray("Fri", 2024)) console.log("Saturday\n",weekdayYearDayArray("Sat", 2024)) console.log("Sunday\n",weekdayYearDayArray("Sun", 2024)) console.log("\n#### 2025 ####") console.log("Monday\n", weekdayYearDayArray("Mon", 2025)) console.log("Tuesday\n", weekdayYearDayArray("Tue", 2025)) console.log("Wednesday\n",weekdayYearDayArray("Wed", 2025)) console.log("Thursday\n",weekdayYearDayArray("Thu", 2025)) console.log("Friday\n",weekdayYearDayArray("Fri", 2025)) console.log("Saturday\n",weekdayYearDayArray("Sat", 2025)) console.log("Sunday\n",weekdayYearDayArray("Sun", 2025)) |
Go ahead and run the code. You should get the following logged arrays:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#### 2024 #### Monday (53) [1, 8, 15, 22, 29, 36, 43, 50, 57, 64, 71, 78, 85, 92, 99, 106, 113, 120, 127, 134, 141, 148, 155, 162, 169, 176, 183, 190, 197, 204, 211, 218, 225, 232, 239, 246, 253, 260, 267, 274, 281, 288, 295, 302, 309, 316, 323, 330, 337, 344, 351, 358, 365] Tuesday (53) [2, 9, 16, 23, 30, 37, 44, 51, 58, 65, 72, 79, 86, 93, 100, 107, 114, 121, 128, 135, 142, 149, 156, 163, 170, 177, 184, 191, 198, 205, 212, 219, 226, 233, 240, 247, 254, 261, 268, 275, 282, 289, 296, 303, 310, 317, 324, 331, 338, 345, 352, 359, 366] Wednesday (52) [3, 10, 17, 24, 31, 38, 45, 52, 59, 66, 73, 80, 87, 94, 101, 108, 115, 122, 129, 136, 143, 150, 157, 164, 171, 178, 185, 192, 199, 206, 213, 220, 227, 234, 241, 248, 255, 262, 269, 276, 283, 290, 297, 304, 311, 318, 325, 332, 339, 346, 353, 360] Thursday (52) [4, 11, 18, 25, 32, 39, 46, 53, 60, 67, 74, 81, 88, 95, 102, 109, 116, 123, 130, 137, 144, 151, 158, 165, 172, 179, 186, 193, 200, 207, 214, 221, 228, 235, 242, 249, 256, 263, 270, 277, 284, 291, 298, 305, 312, 319, 326, 333, 340, 347, 354, 361] Friday (52) [5, 12, 19, 26, 33, 40, 47, 54, 61, 68, 75, 82, 89, 96, 103, 110, 117, 124, 131, 138, 145, 152, 159, 166, 173, 180, 187, 194, 201, 208, 215, 222, 229, 236, 243, 250, 257, 264, 271, 278, 285, 292, 299, 306, 313, 320, 327, 334, 341, 348, 355, 362] Saturday (52) [6, 13, 20, 27, 34, 41, 48, 55, 62, 69, 76, 83, 90, 97, 104, 111, 118, 125, 132, 139, 146, 153, 160, 167, 174, 181, 188, 195, 202, 209, 216, 223, 230, 237, 244, 251, 258, 265, 272, 279, 286, 293, 300, 307, 314, 321, 328, 335, 342, 349, 356, 363] Sunday (52) [7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, 126, 133, 140, 147, 154, 161, 168, 175, 182, 189, 196, 203, 210, 217, 224, 231, 238, 245, 252, 259, 266, 273, 280, 287, 294, 301, 308, 315, 322, 329, 336, 343, 350, 357, 364] #### 2025 #### Monday (52) [6, 13, 20, 27, 34, 41, 48, 55, 62, 69, 76, 83, 90, 97, 104, 111, 118, 125, 132, 139, 146, 153, 160, 167, 174, 181, 188, 195, 202, 209, 216, 223, 230, 237, 244, 251, 258, 265, 272, 279, 286, 293, 300, 307, 314, 321, 328, 335, 342, 349, 356, 363] Tuesday (52) [7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, 126, 133, 140, 147, 154, 161, 168, 175, 182, 189, 196, 203, 210, 217, 224, 231, 238, 245, 252, 259, 266, 273, 280, 287, 294, 301, 308, 315, 322, 329, 336, 343, 350, 357, 364] Wednesday (53) [1, 8, 15, 22, 29, 36, 43, 50, 57, 64, 71, 78, 85, 92, 99, 106, 113, 120, 127, 134, 141, 148, 155, 162, 169, 176, 183, 190, 197, 204, 211, 218, 225, 232, 239, 246, 253, 260, 267, 274, 281, 288, 295, 302, 309, 316, 323, 330, 337, 344, 351, 358, 365] Thursday (52) [2, 9, 16, 23, 30, 37, 44, 51, 58, 65, 72, 79, 86, 93, 100, 107, 114, 121, 128, 135, 142, 149, 156, 163, 170, 177, 184, 191, 198, 205, 212, 219, 226, 233, 240, 247, 254, 261, 268, 275, 282, 289, 296, 303, 310, 317, 324, 331, 338, 345, 352, 359] Friday (52) [3, 10, 17, 24, 31, 38, 45, 52, 59, 66, 73, 80, 87, 94, 101, 108, 115, 122, 129, 136, 143, 150, 157, 164, 171, 178, 185, 192, 199, 206, 213, 220, 227, 234, 241, 248, 255, 262, 269, 276, 283, 290, 297, 304, 311, 318, 325, 332, 339, 346, 353, 360] Saturday (52) [4, 11, 18, 25, 32, 39, 46, 53, 60, 67, 74, 81, 88, 95, 102, 109, 116, 123, 130, 137, 144, 151, 158, 165, 172, 179, 186, 193, 200, 207, 214, 221, 228, 235, 242, 249, 256, 263, 270, 277, 284, 291, 298, 305, 312, 319, 326, 333, 340, 347, 354, 361] Sunday (52) [5, 12, 19, 26, 33, 40, 47, 54, 61, 68, 75, 82, 89, 96, 103, 110, 117, 124, 131, 138, 145, 152, 159, 166, 173, 180, 187, 194, 201, 208, 215, 222, 229, 236, 243, 250, 257, 264, 271, 278, 285, 292, 299, 306, 313, 320, 327, 334, 341, 348, 355, 362] |
Arguments
We’re a little more helpful in this function for the user arguments:
weekday
–Number 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.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.
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.
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.