Javascript calculate the day of the year (1 - 366)
How do I use javascript to calculate the day of the year, from 1 - 366? For example:
January 3 should be 3.
February 1 should be 32.
javascript
|
show 3 more comments
How do I use javascript to calculate the day of the year, from 1 - 366? For example:
January 3 should be 3.
February 1 should be 32.
javascript
4
var days = new Date().getFullYear() % 4 == 0 ? 366 : 365;
– Alex Turpin
Dec 23 '11 at 19:27
But really I'm not sure what you mean. You just want the number of days in the year? Or between two dates?
– Alex Turpin
Dec 23 '11 at 19:28
12
fyi @xeon06, leap year calculation is a bit more complicated than mod'ing by 4. see: leap year algorithm
– Matt Felzani
Dec 23 '11 at 19:29
4
@Xeon06: That's correct only most of the time. From Wikipedia: Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years.
– Cameron
Dec 23 '11 at 19:30
Ah well I stand corrected. @minitech seems to have the right answer then.
– Alex Turpin
Dec 23 '11 at 19:31
|
show 3 more comments
How do I use javascript to calculate the day of the year, from 1 - 366? For example:
January 3 should be 3.
February 1 should be 32.
javascript
How do I use javascript to calculate the day of the year, from 1 - 366? For example:
January 3 should be 3.
February 1 should be 32.
javascript
javascript
edited Feb 14 '14 at 0:55
Kirk Woll
61.8k17159173
61.8k17159173
asked Dec 23 '11 at 19:25
user503853
4
var days = new Date().getFullYear() % 4 == 0 ? 366 : 365;
– Alex Turpin
Dec 23 '11 at 19:27
But really I'm not sure what you mean. You just want the number of days in the year? Or between two dates?
– Alex Turpin
Dec 23 '11 at 19:28
12
fyi @xeon06, leap year calculation is a bit more complicated than mod'ing by 4. see: leap year algorithm
– Matt Felzani
Dec 23 '11 at 19:29
4
@Xeon06: That's correct only most of the time. From Wikipedia: Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years.
– Cameron
Dec 23 '11 at 19:30
Ah well I stand corrected. @minitech seems to have the right answer then.
– Alex Turpin
Dec 23 '11 at 19:31
|
show 3 more comments
4
var days = new Date().getFullYear() % 4 == 0 ? 366 : 365;
– Alex Turpin
Dec 23 '11 at 19:27
But really I'm not sure what you mean. You just want the number of days in the year? Or between two dates?
– Alex Turpin
Dec 23 '11 at 19:28
12
fyi @xeon06, leap year calculation is a bit more complicated than mod'ing by 4. see: leap year algorithm
– Matt Felzani
Dec 23 '11 at 19:29
4
@Xeon06: That's correct only most of the time. From Wikipedia: Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years.
– Cameron
Dec 23 '11 at 19:30
Ah well I stand corrected. @minitech seems to have the right answer then.
– Alex Turpin
Dec 23 '11 at 19:31
4
4
var days = new Date().getFullYear() % 4 == 0 ? 366 : 365;
– Alex Turpin
Dec 23 '11 at 19:27
var days = new Date().getFullYear() % 4 == 0 ? 366 : 365;
– Alex Turpin
Dec 23 '11 at 19:27
But really I'm not sure what you mean. You just want the number of days in the year? Or between two dates?
– Alex Turpin
Dec 23 '11 at 19:28
But really I'm not sure what you mean. You just want the number of days in the year? Or between two dates?
– Alex Turpin
Dec 23 '11 at 19:28
12
12
fyi @xeon06, leap year calculation is a bit more complicated than mod'ing by 4. see: leap year algorithm
– Matt Felzani
Dec 23 '11 at 19:29
fyi @xeon06, leap year calculation is a bit more complicated than mod'ing by 4. see: leap year algorithm
– Matt Felzani
Dec 23 '11 at 19:29
4
4
@Xeon06: That's correct only most of the time. From Wikipedia: Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years.
– Cameron
Dec 23 '11 at 19:30
@Xeon06: That's correct only most of the time. From Wikipedia: Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years.
– Cameron
Dec 23 '11 at 19:30
Ah well I stand corrected. @minitech seems to have the right answer then.
– Alex Turpin
Dec 23 '11 at 19:31
Ah well I stand corrected. @minitech seems to have the right answer then.
– Alex Turpin
Dec 23 '11 at 19:31
|
show 3 more comments
18 Answers
18
active
oldest
votes
Following OP's edit:
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
Edit: The code above will fail when now
is a date in between march 26th and October 29th andnow
's time is before 1AM (eg 00:59:59). This is due to the code not taking daylight savings time into account. You should compensate for this:
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
3
Math.floor
consistently gave me a result of 1 day less than expected after a certain day in April.Math.ceil
worked as expected, but I have seen it be advised that you useMath.round
instead of either.
– baacke
Jan 15 '14 at 17:45
3
The day component is base-1. i.e. to represent January 1st of this year, you'd usenew Date(2014, 0, 1)
, and notnew Date(2014, 0, 0)
as you have here. Is that intentional? Perhaps that's what accounts for being off by one day asnew Date(2014, 0, 0)
will return12/31/2013
.
– Kirk Woll
Feb 14 '14 at 0:58
1
Maybe use.setUTCHours
andDate.UTC()
for a more reliable solution.
– Noyo
Oct 13 '14 at 14:03
6
@AlexTurpin, @T30: I know this is a bit old, but if you were wondering... the problem is caused because of Daylight Savings Time starting in March. This is because when you take the difference of a midnight of a date before DST and midnight of a date after DST you will not have a number of milliseconds evenly divisible by 1000 * 60 * 60 * 24 (it will be exactly one hour off). The easiest solution is to useceil
rather thanfloor
, this will give you a numbering system where Jan 1st = 1. If you want Jan 1st = 0 (asfloor
would give you), just subtract 1 from your final result.
– Warren R.
Nov 4 '14 at 0:37
2
To take timezones and daylight savings into account, change line 3 to:var diff = now - start + (start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000;
– Marlon
Apr 13 '15 at 12:16
|
show 8 more comments
This works across Daylight Savings Time changes in all countries (the "noon" one above doesn't work in Australia):
Date.prototype.isLeapYear = function() {
var year = this.getFullYear();
if((year & 3) != 0) return false;
return ((year % 100) != 0 || (year % 400) == 0);
};
// Get Day of Year
Date.prototype.getDOY = function() {
var dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
var mn = this.getMonth();
var dn = this.getDate();
var dayOfYear = dayCount[mn] + dn;
if(mn > 1 && this.isLeapYear()) dayOfYear++;
return dayOfYear;
};
1
Agree that there are daylight savings related bugs in the accepted answer. The above solution is better and is faster. Here is a variation of it I tested on jsPerf jsperf.com/date-getdayofyear-perf
– Shyam Habarakada
Dec 11 '14 at 18:39
Very nice solution. Elegant and simple.
– Jon
Apr 7 '15 at 0:45
if((year & 3) != 0) return false; should be if((year & 4) != 0) return false;
– Michele Locati
Jun 19 '15 at 8:51
2
@MicheleLocati if((year & 3) != 0) is correct. In binary, all numbers that are not a multiple of four will have either the one or two bit set. That line of code is eliminating all numbers that are not multiple of four by masking off everything but the two and one bits. I'm not sure why he's not using if(year % 4 != 0), though.
– AaronSieb
Jul 1 '15 at 13:19
@AaronSieb Whoops, you're right. I read those "&" as "%"... I need some holidays ;)
– Michele Locati
Jul 2 '15 at 13:57
|
show 1 more comment
I find it very interesting that no one considered using UTC since it is not subject to DST. Therefore, I propose the following:
function daysIntoYear(date){
return (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) - Date.UTC(date.getFullYear(), 0, 0)) / 24 / 60 / 60 / 1000;
}
You can test it with the following:
[new Date(2016,0,1), new Date(2016,1,1), new Date(2016,2,1), new Date(2016,5,1), new Date(2016,11,31)]
.forEach(d =>
console.log(`${d.toLocaleDateString()} is ${daysIntoYear(d)} days into the year`));
Which outputs for the leap year 2016 (verified using http://www.epochconverter.com/days/2016):
1/1/2016 is 1 days into the year
2/1/2016 is 32 days into the year
3/1/2016 is 61 days into the year
6/1/2016 is 153 days into the year
12/31/2016 is 366 days into the year
add a comment |
Date.prototype.dayOfYear= function(){
var j1= new Date(this);
j1.setMonth(0, 0);
return Math.round((this-j1)/8.64e7);
}
alert(new Date().dayOfYear())
2
This would not pass lint... Modifying an object that is not yours, specially a global one. This would also fail in planets with extreme tilt where daylight savings time is over 12 hours. But more realistically, if you are coding for a browser that allows changing the Date object's timezone, j1's timezone could be Australia and this's timezone could be Alaska, breaking the rounding.
– Ray Foss
Mar 10 '16 at 15:30
add a comment |
Luckily this question doesn't specify if the number of the current day is required, leaving room for this answer.
Also some answers (also on other questions) had leap-year problems or used the Date-object. Although javascript's Date object
covers approximately 285616 years (100,000,000 days) on either side of January 1 1970, I was fed up with all kinds of unexpected date inconsistencies across different browsers (most notably year 0 to 99). I was also curious how to calculate it.
So I wrote a simple and above all, small algorithm to calculate the correct (Proleptic Gregorian / Astronomical / ISO 8601:2004 (clause 4.3.2.1), so year 0
exists and is a leap year and negative years are supported) day of the year based on year, month and day.
Note that in AD/BC
notation, year 0 AD/BC does not exist: instead year 1 BC
is the leap-year! IF you need to account for BC notation then simply subtract one year of the (otherwise positive) year-value first!!
I modified (for javascript) the short-circuit bitmask-modulo leapYear algorithm and came up with a magic number to do a bit-wise lookup of offsets (that excludes jan and feb, thus needing 10 * 3 bits (30 bits is less than 31 bits, so we can safely save another character on the bitshift instead of >>>
)).
Note that neither month or day may be 0
. That means that if you need this equation just for the current day (feeding it using .getMonth()
) you just need to remove the --
from --m
.
Note this assumes a valid date (although error-checking is just some characters more).
function dayNo(y,m,d){
return --m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+d;
}
<!-- some examples for the snippet -->
<input type=text value="(-)Y-M-D" onblur="
var d=this.value.match(/(-?d+)[^d]+(dd?)[^d]+(dd?)/)||;
this.nextSibling.innerHTML=' Day: ' + dayNo(+d[1], +d[2], +d[3]);
" /><span></span>
<br><hr><br>
<button onclick="
var d=new Date();
this.nextSibling.innerHTML=dayNo(d.getFullYear(), d.getMonth()+1, d.getDate()) + ' Day(s)';
">get current dayno:</button><span></span>
Here is the version with correct range-validation.
function dayNo(y,m,d){
return --m>=0 && m<12 && d>0 && d<29+(
4*(y=y&3||!(y%25)&&y&15?0:1)+15662003>>m*2&3
) && m*31-(m>1?(1054267675>>m*3-6&7)-y:0)+d;
}
<!-- some examples for the snippet -->
<input type=text value="(-)Y-M-D" onblur="
var d=this.value.match(/(-?d+)[^d]+(dd?)[^d]+(dd?)/)||;
this.nextSibling.innerHTML=' Day: ' + dayNo(+d[1], +d[2], +d[3]);
" /><span></span>
Again, one line, but I split it into 3 lines for readability (and following explanation).
The last line is identical to the function above, however the (identical) leapYear algorithm is moved to a previous short-circuit section (before the day-number calculation), because it is also needed to know how much days a month has in a given (leap) year.
The middle line calculates the correct offset number (for max number of days) for a given month in a given (leap)year using another magic number: since 31-28=3
and 3
is just 2 bits, then 12*2=24
bits, we can store all 12 months. Since addition can be faster then subtraction, we add the offset (instead of subtract it from 31
). To avoid a leap-year decision-branch for February, we modify that magic lookup-number on the fly.
That leaves us with the (pretty obvious) first line: it checks that month and date are within valid bounds and ensures us with a false
return value on range error (note that this function also should not be able to return 0, because 1 jan 0000 is still day 1.), providing easy error-checking: if(r=dayNo(/*y, m, d*/)){}
.
If used this way (where month and day may not be 0
), then one can change --m>=0 && m<12
to m>0 && --m<12
(saving another char).
The reason I typed the snippet in it's current form is that for 0-based month values, one just needs to remove the --
from --m
.
Extra:
Note, don't use this day's per month algorithm if you need just max day's per month. In that case there is a more efficient algorithm (because we only need leepYear when the month is February) I posted as answer this question: What is the best way to determine the number of days in a month with javascript?.
1
I threw it into a date function for ease of use. Granted the Date() inconsistencies are still an issue, but I'm using years 2015+ so I hope they are consistent. Poor JSHint died trying to validate your code hahah.Date.prototype.dayNo = function(){ var y = this.getFullYear(); var m = this.getMonth()+1; var d = this.getDate(); return --m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+d; };
– freshvolk
Feb 2 '15 at 20:36
Glad you liked it. This algo is fully tested for 2^31-1 years (a whopping 2147483647, that's >7500 times the range of javascript's date-object) on both sides of 0. (It might work for 2^32, but I have not tested that yet). Also, you might read my answer again: you can shave off+1
fromthis.getMonth()+1
if you remove the--
from--m
. EDIT So, I would do (for a library):Date.prototype.dayNo = function(){ var y=this.getFullYear(), m=this.getMonth(); return m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+this.getDate(); };
– GitaarLAB
Feb 2 '15 at 21:09
I actually just did that when I was looking at it! I realized I was adding one then subtracting it immediately. I think that by the time we get to2^31 - 2016
years from now js will probably be a little outdated.
– freshvolk
Feb 2 '15 at 21:12
@Freshvolk: lol, which is why I never tested it beyond 2^31:)
But until then, it will at least give predictable and consistent results haha. EDIT, just for theory's sake, using the slower conventional all-modulo algo for leapyear, the range can be extended to 2^53. The month-lookup algo is not the limiting factor.
– GitaarLAB
Feb 2 '15 at 21:27
add a comment |
Well, if I understand you correctly, you want 366 on a leap year, 365 otherwise, right? A year is a leap year if it's evenly divisible by 4 but not by 100 unless it's also divisible by 400:
function daysInYear(year) {
if(year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
// Leap year
return 366;
} else {
// Not a leap year
return 365;
}
}
Edit after update:
In that case, I don't think there's a built-in method; you'll need to do this:
function daysInFebruary(year) {
if(year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
// Leap year
return 29;
} else {
// Not a leap year
return 28;
}
}
function dateToDay(date) {
var feb = daysInFebruary(date.getFullYear());
var aggregateMonths = [0, // January
31, // February
31 + feb, // March
31 + feb + 31, // April
31 + feb + 31 + 30, // May
31 + feb + 31 + 30 + 31, // June
31 + feb + 31 + 30 + 31 + 30, // July
31 + feb + 31 + 30 + 31 + 30 + 31, // August
31 + feb + 31 + 30 + 31 + 30 + 31 + 31, // September
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30, // October
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, // November
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, // December
];
return aggregateMonths[date.getMonth()] + date.getDate();
}
(Yes, I actually did that without copying or pasting. If there's an easy way I'll be mad)
My easy way is just using magic number 1054267675 and call it a day, I'm way to lazy to type all that:)
– GitaarLAB
Jan 6 '15 at 2:25
add a comment |
This is a simple way to find the current day in the year, and it should account for leap years without a problem:
Javascript:
Math.round((new Date().setHours(23) - new Date(new Date().getYear()+1900, 0, 1, 0, 0, 0))/1000/60/60/24);
Javascript in Google Apps Script:
Math.round((new Date().setHours(23) - new Date(new Date().getYear(), 0, 1, 0, 0, 0))/1000/60/60/24);
The primary action of this code is to find the number of milliseconds that have elapsed in the current year and then convert this number into days. The number of milliseconds that have elapsed in the current year can be found by subtracting the number of milliseconds of the first second of the first day of the current year, which is obtained with new Date(new Date().getYear()+1900, 0, 1, 0, 0, 0)
(Javascript) or new Date(new Date().getYear(), 0, 1, 0, 0, 0)
(Google Apps Script), from the milliseconds of the 23rd hour of the current day, which was found with new Date().setHours(23)
. The purpose of setting the current date to the 23rd hour is to ensure that the day of year is rounded correctly by Math.round()
.
Once you have the number of milliseconds of the current year, then you can convert this time into days by dividing by 1000 to convert milliseconds to seconds, then dividing by 60 to convert seconds to minutes, then dividing by 60 to convert minutes to hours, and finally dividing by 24 to convert hours to days.
Note: This post was edited to account for differences between JavaScript and JavaScript implemented in Google Apps Script. Also, more context was added for the answer.
Can you please explain a little bit more than just saying it's working?
– Stephen Reindl
Mar 7 '15 at 19:51
Yes! I am sorry for not providing more detail in the first place. The expression inside the "Math.round" statement finds the number of milliseconds in the current year by subtracting the first day of the year from the milliseconds of the last hour of the current day of the year. The number of milliseconds is then divided by 1000 to convert to seconds, 60 to convert to minutes, 60 to convert to hours, and 24 to convert to days. The expression is wrapped within a "Math.round()" function so that it rounds to an integer for the day of year.
– Liz Page-Gould
Mar 7 '15 at 20:13
I should also add that this solution is essentially the same as the "accepted" solution, except it does everything in one line of code.
– Liz Page-Gould
Mar 7 '15 at 20:45
How does this account for daylight savings time? Wouldn't that throw it off a bit?
– Dan Oswalt
Mar 7 '15 at 22:34
No, daylight savings time would not affect this calculation, because daylight savings time has the resolution of hours and this code is calculating the number of days in the year. Moreover, because I used the.setHours
method on the first date object and specified the time of day with the second date object, a change of 1 hour from daylight savings time will not affect the times of the Date objects used in this calculation.
– Liz Page-Gould
Mar 8 '15 at 14:45
add a comment |
This method takes into account timezone issue and daylight saving time
function dayofyear(d) { // d is a Date object
var yn = d.getFullYear();
var mn = d.getMonth();
var dn = d.getDate();
var d1 = new Date(yn,0,1,12,0,0); // noon on Jan. 1
var d2 = new Date(yn,mn,dn,12,0,0); // noon on input date
var ddiff = Math.round((d2-d1)/864e5);
return ddiff+1;
}
(took from here)
See also this fiddle
This is a bit inefficient, but it works perfectly. You could just get time the objects and setDate change the date to day 1. Both of which are perfectly supported. The idea is you wont be moving around timezones. This also assumes that daylight savings time is less than 12 hours... which is a safe assumption on earth.
– Ray Foss
Mar 10 '16 at 15:55
add a comment |
If you don't want to re-invent the wheel, you can use the excellent date-fns (node.js) library:
var getDayOfYear = require('date-fns/get_day_of_year')
var dayOfYear = getDayOfYear(new Date(2017, 1, 1)) // 1st february => 32
add a comment |
Math.round((new Date().setHours(23) - new Date(new Date().getFullYear(), 0, 1, 0, 0, 0))/1000/86400);
further optimizes the answer.
Moreover, by changing setHours(23) or the last-but-two zero later on to another value may provide day-of-year related to another timezone.
For example, to retrieve from Europe a resource located in America.
add a comment |
I think this is more straightforward:
var date365 = 0;
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var currentMonth = currentDate.getMonth();
var currentDay = currentDate.getDate();
var monthLength = [31,28,31,30,31,30,31,31,30,31,30,31];
var leapYear = new Date(currentYear, 1, 29);
if (leapYear.getDate() == 29) { // If it's a leap year, changes 28 to 29
monthLength[1] = 29;
}
for ( i=0; i < currentMonth; i++ ) {
date365 = date365 + monthLength[i];
}
date365 = date365 + currentDay; // Done!
add a comment |
I've made one that's readable and will do the trick very quickly, as well as handle JS Date objects with disparate time zones.
I've included quite a few test cases for time zones, DST, leap seconds and Leap years.
P.S. ECMA-262 ignores leap seconds, unlike UTC. If you were to convert this to a language that uses real UTC, you could just add 1 to oneDay
.
// returns 1 - 366
findDayOfYear = function (date) {
var oneDay = 1000 * 60 * 60 * 24; // A day in milliseconds
var og = { // Saving original data
ts: date.getTime(),
dom: date.getDate(), // We don't need to save hours/minutes because DST is never at 12am.
month: date.getMonth()
}
date.setDate(1); // Sets Date of the Month to the 1st.
date.setMonth(0); // Months are zero based in JS's Date object
var start_ts = date.getTime(); // New Year's Midnight JS Timestamp
var diff = og.ts - start_ts;
date.setDate(og.dom); // Revert back to original date object
date.setMonth(og.month); // This method does preserve timezone
return Math.round(diff / oneDay) + 1; // Deals with DST globally. Ceil fails in Australia. Floor Fails in US.
}
// Tests
var pre_start_dst = new Date(2016, 2, 12);
var on_start_dst = new Date(2016, 2, 13);
var post_start_dst = new Date(2016, 2, 14);
var pre_end_dst_date = new Date(2016, 10, 5);
var on_end_dst_date = new Date(2016, 10, 6);
var post_end_dst_date = new Date(2016, 10, 7);
var pre_leap_second = new Date(2015, 5, 29);
var on_leap_second = new Date(2015, 5, 30);
var post_leap_second = new Date(2015, 6, 1);
// 2012 was a leap year with a leap second in june 30th
var leap_second_december31_premidnight = new Date(2012, 11, 31, 23, 59, 59, 999);
var january1 = new Date(2016, 0, 1);
var january31 = new Date(2016, 0, 31);
var december31 = new Date(2015, 11, 31);
var leap_december31 = new Date(2016, 11, 31);
alert( ""
+ "nPre Start DST: " + findDayOfYear(pre_start_dst) + " === 72"
+ "nOn Start DST: " + findDayOfYear(on_start_dst) + " === 73"
+ "nPost Start DST: " + findDayOfYear(post_start_dst) + " === 74"
+ "nPre Leap Second: " + findDayOfYear(pre_leap_second) + " === 180"
+ "nOn Leap Second: " + findDayOfYear(on_leap_second) + " === 181"
+ "nPost Leap Second: " + findDayOfYear(post_leap_second) + " === 182"
+ "nPre End DST: " + findDayOfYear(pre_end_dst_date) + " === 310"
+ "nOn End DST: " + findDayOfYear(on_end_dst_date) + " === 311"
+ "nPost End DST: " + findDayOfYear(post_end_dst_date) + " === 312"
+ "nJanuary 1st: " + findDayOfYear(january1) + " === 1"
+ "nJanuary 31st: " + findDayOfYear(january31) + " === 31"
+ "nNormal December 31st: " + findDayOfYear(december31) + " === 365"
+ "nLeap December 31st: " + findDayOfYear(leap_december31) + " === 366"
+ "nLast Second of Double Leap: " + findDayOfYear(leap_second_december31_premidnight) + " === 366"
);
add a comment |
I would like to provide a solution that does calculations adding the days for each previous month:
function getDayOfYear(date) {
var month = date.getMonth();
var year = date.getFullYear();
var days = date.getDate();
for (var i = 0; i < month; i++) {
days += new Date(year, i+1, 0).getDate();
}
return days;
}
var input = new Date(2017, 7, 5);
console.log(input);
console.log(getDayOfYear(input));
This way you don't have to manage the details of leap years and daylight saving.
add a comment |
A alternative using UTC timestamps. Also as others noted the day indicating 1st a month is 1 rather than 0. The month starts at 0 however.
var now = Date.now();
var year = new Date().getUTCFullYear();
var year_start = Date.UTC(year, 0, 1);
var day_length_in_ms = 1000*60*60*24;
var day_number = Math.floor((now - year_start)/day_length_in_ms)
console.log("Day of year " + day_number);
add a comment |
You can pass parameter as date number in setDate
function:
var targetDate = new Date();
targetDate.setDate(1);
// Now we can see the expected date as: Mon Jan 01 2018 01:43:24
console.log(targetDate);
targetDate.setDate(365);
// You can see: Mon Dec 31 2018 01:44:47
console.log(targetDate)
add a comment |
This might be useful to those who need the day of the year as a string and have jQuery UI available.
You can use jQuery UI Datepicker:
day_of_year_string = $.datepicker.formatDate("o", new Date())
Underneath it works the same way as some of the answers already mentioned ((date_ms - first_date_of_year_ms) / ms_per_day
):
function getDayOfTheYearFromDate(d) {
return Math.round((new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime()
- new Date(d.getFullYear(), 0, 0).getTime()) / 86400000);
}
day_of_year_int = getDayOfTheYearFromDate(new Date())
add a comment |
It always get's me worried when mixing maths with date functions (it's so easy to miss some leap year other detail). Say you have:
var d = new Date();
I would suggest using the following, days will be saved in day
:
for(var day = d.getDate(); d.getMonth(); day += d.getDate())
d.setDate(0);
Can't see any reason why this wouldn't work just fine (and I wouldn't be so worried about the few iterations since this will not be used so intensively).
add a comment |
/*USE THIS SCRIPT */
var today = new Date();
var first = new Date(today.getFullYear(), 0, 1);
var theDay = Math.round(((today - first) / 1000 / 60 / 60 / 24) + .5, 0);
alert("Today is the " + theDay + (theDay == 1 ? "st" : (theDay == 2 ? "nd" : (theDay == 3 ? "rd" : "th"))) + " day of the year");
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8619879%2fjavascript-calculate-the-day-of-the-year-1-366%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
18 Answers
18
active
oldest
votes
18 Answers
18
active
oldest
votes
active
oldest
votes
active
oldest
votes
Following OP's edit:
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
Edit: The code above will fail when now
is a date in between march 26th and October 29th andnow
's time is before 1AM (eg 00:59:59). This is due to the code not taking daylight savings time into account. You should compensate for this:
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
3
Math.floor
consistently gave me a result of 1 day less than expected after a certain day in April.Math.ceil
worked as expected, but I have seen it be advised that you useMath.round
instead of either.
– baacke
Jan 15 '14 at 17:45
3
The day component is base-1. i.e. to represent January 1st of this year, you'd usenew Date(2014, 0, 1)
, and notnew Date(2014, 0, 0)
as you have here. Is that intentional? Perhaps that's what accounts for being off by one day asnew Date(2014, 0, 0)
will return12/31/2013
.
– Kirk Woll
Feb 14 '14 at 0:58
1
Maybe use.setUTCHours
andDate.UTC()
for a more reliable solution.
– Noyo
Oct 13 '14 at 14:03
6
@AlexTurpin, @T30: I know this is a bit old, but if you were wondering... the problem is caused because of Daylight Savings Time starting in March. This is because when you take the difference of a midnight of a date before DST and midnight of a date after DST you will not have a number of milliseconds evenly divisible by 1000 * 60 * 60 * 24 (it will be exactly one hour off). The easiest solution is to useceil
rather thanfloor
, this will give you a numbering system where Jan 1st = 1. If you want Jan 1st = 0 (asfloor
would give you), just subtract 1 from your final result.
– Warren R.
Nov 4 '14 at 0:37
2
To take timezones and daylight savings into account, change line 3 to:var diff = now - start + (start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000;
– Marlon
Apr 13 '15 at 12:16
|
show 8 more comments
Following OP's edit:
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
Edit: The code above will fail when now
is a date in between march 26th and October 29th andnow
's time is before 1AM (eg 00:59:59). This is due to the code not taking daylight savings time into account. You should compensate for this:
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
3
Math.floor
consistently gave me a result of 1 day less than expected after a certain day in April.Math.ceil
worked as expected, but I have seen it be advised that you useMath.round
instead of either.
– baacke
Jan 15 '14 at 17:45
3
The day component is base-1. i.e. to represent January 1st of this year, you'd usenew Date(2014, 0, 1)
, and notnew Date(2014, 0, 0)
as you have here. Is that intentional? Perhaps that's what accounts for being off by one day asnew Date(2014, 0, 0)
will return12/31/2013
.
– Kirk Woll
Feb 14 '14 at 0:58
1
Maybe use.setUTCHours
andDate.UTC()
for a more reliable solution.
– Noyo
Oct 13 '14 at 14:03
6
@AlexTurpin, @T30: I know this is a bit old, but if you were wondering... the problem is caused because of Daylight Savings Time starting in March. This is because when you take the difference of a midnight of a date before DST and midnight of a date after DST you will not have a number of milliseconds evenly divisible by 1000 * 60 * 60 * 24 (it will be exactly one hour off). The easiest solution is to useceil
rather thanfloor
, this will give you a numbering system where Jan 1st = 1. If you want Jan 1st = 0 (asfloor
would give you), just subtract 1 from your final result.
– Warren R.
Nov 4 '14 at 0:37
2
To take timezones and daylight savings into account, change line 3 to:var diff = now - start + (start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000;
– Marlon
Apr 13 '15 at 12:16
|
show 8 more comments
Following OP's edit:
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
Edit: The code above will fail when now
is a date in between march 26th and October 29th andnow
's time is before 1AM (eg 00:59:59). This is due to the code not taking daylight savings time into account. You should compensate for this:
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
Following OP's edit:
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
Edit: The code above will fail when now
is a date in between march 26th and October 29th andnow
's time is before 1AM (eg 00:59:59). This is due to the code not taking daylight savings time into account. You should compensate for this:
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
edited Oct 8 '17 at 22:20
Koen Peters
11k53049
11k53049
answered Dec 23 '11 at 19:36
Alex TurpinAlex Turpin
34.1k1993131
34.1k1993131
3
Math.floor
consistently gave me a result of 1 day less than expected after a certain day in April.Math.ceil
worked as expected, but I have seen it be advised that you useMath.round
instead of either.
– baacke
Jan 15 '14 at 17:45
3
The day component is base-1. i.e. to represent January 1st of this year, you'd usenew Date(2014, 0, 1)
, and notnew Date(2014, 0, 0)
as you have here. Is that intentional? Perhaps that's what accounts for being off by one day asnew Date(2014, 0, 0)
will return12/31/2013
.
– Kirk Woll
Feb 14 '14 at 0:58
1
Maybe use.setUTCHours
andDate.UTC()
for a more reliable solution.
– Noyo
Oct 13 '14 at 14:03
6
@AlexTurpin, @T30: I know this is a bit old, but if you were wondering... the problem is caused because of Daylight Savings Time starting in March. This is because when you take the difference of a midnight of a date before DST and midnight of a date after DST you will not have a number of milliseconds evenly divisible by 1000 * 60 * 60 * 24 (it will be exactly one hour off). The easiest solution is to useceil
rather thanfloor
, this will give you a numbering system where Jan 1st = 1. If you want Jan 1st = 0 (asfloor
would give you), just subtract 1 from your final result.
– Warren R.
Nov 4 '14 at 0:37
2
To take timezones and daylight savings into account, change line 3 to:var diff = now - start + (start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000;
– Marlon
Apr 13 '15 at 12:16
|
show 8 more comments
3
Math.floor
consistently gave me a result of 1 day less than expected after a certain day in April.Math.ceil
worked as expected, but I have seen it be advised that you useMath.round
instead of either.
– baacke
Jan 15 '14 at 17:45
3
The day component is base-1. i.e. to represent January 1st of this year, you'd usenew Date(2014, 0, 1)
, and notnew Date(2014, 0, 0)
as you have here. Is that intentional? Perhaps that's what accounts for being off by one day asnew Date(2014, 0, 0)
will return12/31/2013
.
– Kirk Woll
Feb 14 '14 at 0:58
1
Maybe use.setUTCHours
andDate.UTC()
for a more reliable solution.
– Noyo
Oct 13 '14 at 14:03
6
@AlexTurpin, @T30: I know this is a bit old, but if you were wondering... the problem is caused because of Daylight Savings Time starting in March. This is because when you take the difference of a midnight of a date before DST and midnight of a date after DST you will not have a number of milliseconds evenly divisible by 1000 * 60 * 60 * 24 (it will be exactly one hour off). The easiest solution is to useceil
rather thanfloor
, this will give you a numbering system where Jan 1st = 1. If you want Jan 1st = 0 (asfloor
would give you), just subtract 1 from your final result.
– Warren R.
Nov 4 '14 at 0:37
2
To take timezones and daylight savings into account, change line 3 to:var diff = now - start + (start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000;
– Marlon
Apr 13 '15 at 12:16
3
3
Math.floor
consistently gave me a result of 1 day less than expected after a certain day in April. Math.ceil
worked as expected, but I have seen it be advised that you use Math.round
instead of either.– baacke
Jan 15 '14 at 17:45
Math.floor
consistently gave me a result of 1 day less than expected after a certain day in April. Math.ceil
worked as expected, but I have seen it be advised that you use Math.round
instead of either.– baacke
Jan 15 '14 at 17:45
3
3
The day component is base-1. i.e. to represent January 1st of this year, you'd use
new Date(2014, 0, 1)
, and not new Date(2014, 0, 0)
as you have here. Is that intentional? Perhaps that's what accounts for being off by one day as new Date(2014, 0, 0)
will return 12/31/2013
.– Kirk Woll
Feb 14 '14 at 0:58
The day component is base-1. i.e. to represent January 1st of this year, you'd use
new Date(2014, 0, 1)
, and not new Date(2014, 0, 0)
as you have here. Is that intentional? Perhaps that's what accounts for being off by one day as new Date(2014, 0, 0)
will return 12/31/2013
.– Kirk Woll
Feb 14 '14 at 0:58
1
1
Maybe use
.setUTCHours
and Date.UTC()
for a more reliable solution.– Noyo
Oct 13 '14 at 14:03
Maybe use
.setUTCHours
and Date.UTC()
for a more reliable solution.– Noyo
Oct 13 '14 at 14:03
6
6
@AlexTurpin, @T30: I know this is a bit old, but if you were wondering... the problem is caused because of Daylight Savings Time starting in March. This is because when you take the difference of a midnight of a date before DST and midnight of a date after DST you will not have a number of milliseconds evenly divisible by 1000 * 60 * 60 * 24 (it will be exactly one hour off). The easiest solution is to use
ceil
rather than floor
, this will give you a numbering system where Jan 1st = 1. If you want Jan 1st = 0 (as floor
would give you), just subtract 1 from your final result.– Warren R.
Nov 4 '14 at 0:37
@AlexTurpin, @T30: I know this is a bit old, but if you were wondering... the problem is caused because of Daylight Savings Time starting in March. This is because when you take the difference of a midnight of a date before DST and midnight of a date after DST you will not have a number of milliseconds evenly divisible by 1000 * 60 * 60 * 24 (it will be exactly one hour off). The easiest solution is to use
ceil
rather than floor
, this will give you a numbering system where Jan 1st = 1. If you want Jan 1st = 0 (as floor
would give you), just subtract 1 from your final result.– Warren R.
Nov 4 '14 at 0:37
2
2
To take timezones and daylight savings into account, change line 3 to:
var diff = now - start + (start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000;
– Marlon
Apr 13 '15 at 12:16
To take timezones and daylight savings into account, change line 3 to:
var diff = now - start + (start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000;
– Marlon
Apr 13 '15 at 12:16
|
show 8 more comments
This works across Daylight Savings Time changes in all countries (the "noon" one above doesn't work in Australia):
Date.prototype.isLeapYear = function() {
var year = this.getFullYear();
if((year & 3) != 0) return false;
return ((year % 100) != 0 || (year % 400) == 0);
};
// Get Day of Year
Date.prototype.getDOY = function() {
var dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
var mn = this.getMonth();
var dn = this.getDate();
var dayOfYear = dayCount[mn] + dn;
if(mn > 1 && this.isLeapYear()) dayOfYear++;
return dayOfYear;
};
1
Agree that there are daylight savings related bugs in the accepted answer. The above solution is better and is faster. Here is a variation of it I tested on jsPerf jsperf.com/date-getdayofyear-perf
– Shyam Habarakada
Dec 11 '14 at 18:39
Very nice solution. Elegant and simple.
– Jon
Apr 7 '15 at 0:45
if((year & 3) != 0) return false; should be if((year & 4) != 0) return false;
– Michele Locati
Jun 19 '15 at 8:51
2
@MicheleLocati if((year & 3) != 0) is correct. In binary, all numbers that are not a multiple of four will have either the one or two bit set. That line of code is eliminating all numbers that are not multiple of four by masking off everything but the two and one bits. I'm not sure why he's not using if(year % 4 != 0), though.
– AaronSieb
Jul 1 '15 at 13:19
@AaronSieb Whoops, you're right. I read those "&" as "%"... I need some holidays ;)
– Michele Locati
Jul 2 '15 at 13:57
|
show 1 more comment
This works across Daylight Savings Time changes in all countries (the "noon" one above doesn't work in Australia):
Date.prototype.isLeapYear = function() {
var year = this.getFullYear();
if((year & 3) != 0) return false;
return ((year % 100) != 0 || (year % 400) == 0);
};
// Get Day of Year
Date.prototype.getDOY = function() {
var dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
var mn = this.getMonth();
var dn = this.getDate();
var dayOfYear = dayCount[mn] + dn;
if(mn > 1 && this.isLeapYear()) dayOfYear++;
return dayOfYear;
};
1
Agree that there are daylight savings related bugs in the accepted answer. The above solution is better and is faster. Here is a variation of it I tested on jsPerf jsperf.com/date-getdayofyear-perf
– Shyam Habarakada
Dec 11 '14 at 18:39
Very nice solution. Elegant and simple.
– Jon
Apr 7 '15 at 0:45
if((year & 3) != 0) return false; should be if((year & 4) != 0) return false;
– Michele Locati
Jun 19 '15 at 8:51
2
@MicheleLocati if((year & 3) != 0) is correct. In binary, all numbers that are not a multiple of four will have either the one or two bit set. That line of code is eliminating all numbers that are not multiple of four by masking off everything but the two and one bits. I'm not sure why he's not using if(year % 4 != 0), though.
– AaronSieb
Jul 1 '15 at 13:19
@AaronSieb Whoops, you're right. I read those "&" as "%"... I need some holidays ;)
– Michele Locati
Jul 2 '15 at 13:57
|
show 1 more comment
This works across Daylight Savings Time changes in all countries (the "noon" one above doesn't work in Australia):
Date.prototype.isLeapYear = function() {
var year = this.getFullYear();
if((year & 3) != 0) return false;
return ((year % 100) != 0 || (year % 400) == 0);
};
// Get Day of Year
Date.prototype.getDOY = function() {
var dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
var mn = this.getMonth();
var dn = this.getDate();
var dayOfYear = dayCount[mn] + dn;
if(mn > 1 && this.isLeapYear()) dayOfYear++;
return dayOfYear;
};
This works across Daylight Savings Time changes in all countries (the "noon" one above doesn't work in Australia):
Date.prototype.isLeapYear = function() {
var year = this.getFullYear();
if((year & 3) != 0) return false;
return ((year % 100) != 0 || (year % 400) == 0);
};
// Get Day of Year
Date.prototype.getDOY = function() {
var dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
var mn = this.getMonth();
var dn = this.getDate();
var dayOfYear = dayCount[mn] + dn;
if(mn > 1 && this.isLeapYear()) dayOfYear++;
return dayOfYear;
};
answered Oct 17 '14 at 14:01
Joe OrostJoe Orost
628611
628611
1
Agree that there are daylight savings related bugs in the accepted answer. The above solution is better and is faster. Here is a variation of it I tested on jsPerf jsperf.com/date-getdayofyear-perf
– Shyam Habarakada
Dec 11 '14 at 18:39
Very nice solution. Elegant and simple.
– Jon
Apr 7 '15 at 0:45
if((year & 3) != 0) return false; should be if((year & 4) != 0) return false;
– Michele Locati
Jun 19 '15 at 8:51
2
@MicheleLocati if((year & 3) != 0) is correct. In binary, all numbers that are not a multiple of four will have either the one or two bit set. That line of code is eliminating all numbers that are not multiple of four by masking off everything but the two and one bits. I'm not sure why he's not using if(year % 4 != 0), though.
– AaronSieb
Jul 1 '15 at 13:19
@AaronSieb Whoops, you're right. I read those "&" as "%"... I need some holidays ;)
– Michele Locati
Jul 2 '15 at 13:57
|
show 1 more comment
1
Agree that there are daylight savings related bugs in the accepted answer. The above solution is better and is faster. Here is a variation of it I tested on jsPerf jsperf.com/date-getdayofyear-perf
– Shyam Habarakada
Dec 11 '14 at 18:39
Very nice solution. Elegant and simple.
– Jon
Apr 7 '15 at 0:45
if((year & 3) != 0) return false; should be if((year & 4) != 0) return false;
– Michele Locati
Jun 19 '15 at 8:51
2
@MicheleLocati if((year & 3) != 0) is correct. In binary, all numbers that are not a multiple of four will have either the one or two bit set. That line of code is eliminating all numbers that are not multiple of four by masking off everything but the two and one bits. I'm not sure why he's not using if(year % 4 != 0), though.
– AaronSieb
Jul 1 '15 at 13:19
@AaronSieb Whoops, you're right. I read those "&" as "%"... I need some holidays ;)
– Michele Locati
Jul 2 '15 at 13:57
1
1
Agree that there are daylight savings related bugs in the accepted answer. The above solution is better and is faster. Here is a variation of it I tested on jsPerf jsperf.com/date-getdayofyear-perf
– Shyam Habarakada
Dec 11 '14 at 18:39
Agree that there are daylight savings related bugs in the accepted answer. The above solution is better and is faster. Here is a variation of it I tested on jsPerf jsperf.com/date-getdayofyear-perf
– Shyam Habarakada
Dec 11 '14 at 18:39
Very nice solution. Elegant and simple.
– Jon
Apr 7 '15 at 0:45
Very nice solution. Elegant and simple.
– Jon
Apr 7 '15 at 0:45
if((year & 3) != 0) return false; should be if((year & 4) != 0) return false;
– Michele Locati
Jun 19 '15 at 8:51
if((year & 3) != 0) return false; should be if((year & 4) != 0) return false;
– Michele Locati
Jun 19 '15 at 8:51
2
2
@MicheleLocati if((year & 3) != 0) is correct. In binary, all numbers that are not a multiple of four will have either the one or two bit set. That line of code is eliminating all numbers that are not multiple of four by masking off everything but the two and one bits. I'm not sure why he's not using if(year % 4 != 0), though.
– AaronSieb
Jul 1 '15 at 13:19
@MicheleLocati if((year & 3) != 0) is correct. In binary, all numbers that are not a multiple of four will have either the one or two bit set. That line of code is eliminating all numbers that are not multiple of four by masking off everything but the two and one bits. I'm not sure why he's not using if(year % 4 != 0), though.
– AaronSieb
Jul 1 '15 at 13:19
@AaronSieb Whoops, you're right. I read those "&" as "%"... I need some holidays ;)
– Michele Locati
Jul 2 '15 at 13:57
@AaronSieb Whoops, you're right. I read those "&" as "%"... I need some holidays ;)
– Michele Locati
Jul 2 '15 at 13:57
|
show 1 more comment
I find it very interesting that no one considered using UTC since it is not subject to DST. Therefore, I propose the following:
function daysIntoYear(date){
return (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) - Date.UTC(date.getFullYear(), 0, 0)) / 24 / 60 / 60 / 1000;
}
You can test it with the following:
[new Date(2016,0,1), new Date(2016,1,1), new Date(2016,2,1), new Date(2016,5,1), new Date(2016,11,31)]
.forEach(d =>
console.log(`${d.toLocaleDateString()} is ${daysIntoYear(d)} days into the year`));
Which outputs for the leap year 2016 (verified using http://www.epochconverter.com/days/2016):
1/1/2016 is 1 days into the year
2/1/2016 is 32 days into the year
3/1/2016 is 61 days into the year
6/1/2016 is 153 days into the year
12/31/2016 is 366 days into the year
add a comment |
I find it very interesting that no one considered using UTC since it is not subject to DST. Therefore, I propose the following:
function daysIntoYear(date){
return (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) - Date.UTC(date.getFullYear(), 0, 0)) / 24 / 60 / 60 / 1000;
}
You can test it with the following:
[new Date(2016,0,1), new Date(2016,1,1), new Date(2016,2,1), new Date(2016,5,1), new Date(2016,11,31)]
.forEach(d =>
console.log(`${d.toLocaleDateString()} is ${daysIntoYear(d)} days into the year`));
Which outputs for the leap year 2016 (verified using http://www.epochconverter.com/days/2016):
1/1/2016 is 1 days into the year
2/1/2016 is 32 days into the year
3/1/2016 is 61 days into the year
6/1/2016 is 153 days into the year
12/31/2016 is 366 days into the year
add a comment |
I find it very interesting that no one considered using UTC since it is not subject to DST. Therefore, I propose the following:
function daysIntoYear(date){
return (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) - Date.UTC(date.getFullYear(), 0, 0)) / 24 / 60 / 60 / 1000;
}
You can test it with the following:
[new Date(2016,0,1), new Date(2016,1,1), new Date(2016,2,1), new Date(2016,5,1), new Date(2016,11,31)]
.forEach(d =>
console.log(`${d.toLocaleDateString()} is ${daysIntoYear(d)} days into the year`));
Which outputs for the leap year 2016 (verified using http://www.epochconverter.com/days/2016):
1/1/2016 is 1 days into the year
2/1/2016 is 32 days into the year
3/1/2016 is 61 days into the year
6/1/2016 is 153 days into the year
12/31/2016 is 366 days into the year
I find it very interesting that no one considered using UTC since it is not subject to DST. Therefore, I propose the following:
function daysIntoYear(date){
return (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) - Date.UTC(date.getFullYear(), 0, 0)) / 24 / 60 / 60 / 1000;
}
You can test it with the following:
[new Date(2016,0,1), new Date(2016,1,1), new Date(2016,2,1), new Date(2016,5,1), new Date(2016,11,31)]
.forEach(d =>
console.log(`${d.toLocaleDateString()} is ${daysIntoYear(d)} days into the year`));
Which outputs for the leap year 2016 (verified using http://www.epochconverter.com/days/2016):
1/1/2016 is 1 days into the year
2/1/2016 is 32 days into the year
3/1/2016 is 61 days into the year
6/1/2016 is 153 days into the year
12/31/2016 is 366 days into the year
answered Dec 5 '16 at 13:46
user2501097user2501097
32124
32124
add a comment |
add a comment |
Date.prototype.dayOfYear= function(){
var j1= new Date(this);
j1.setMonth(0, 0);
return Math.round((this-j1)/8.64e7);
}
alert(new Date().dayOfYear())
2
This would not pass lint... Modifying an object that is not yours, specially a global one. This would also fail in planets with extreme tilt where daylight savings time is over 12 hours. But more realistically, if you are coding for a browser that allows changing the Date object's timezone, j1's timezone could be Australia and this's timezone could be Alaska, breaking the rounding.
– Ray Foss
Mar 10 '16 at 15:30
add a comment |
Date.prototype.dayOfYear= function(){
var j1= new Date(this);
j1.setMonth(0, 0);
return Math.round((this-j1)/8.64e7);
}
alert(new Date().dayOfYear())
2
This would not pass lint... Modifying an object that is not yours, specially a global one. This would also fail in planets with extreme tilt where daylight savings time is over 12 hours. But more realistically, if you are coding for a browser that allows changing the Date object's timezone, j1's timezone could be Australia and this's timezone could be Alaska, breaking the rounding.
– Ray Foss
Mar 10 '16 at 15:30
add a comment |
Date.prototype.dayOfYear= function(){
var j1= new Date(this);
j1.setMonth(0, 0);
return Math.round((this-j1)/8.64e7);
}
alert(new Date().dayOfYear())
Date.prototype.dayOfYear= function(){
var j1= new Date(this);
j1.setMonth(0, 0);
return Math.round((this-j1)/8.64e7);
}
alert(new Date().dayOfYear())
edited Jan 20 '16 at 21:46
insertusernamehere
19k668105
19k668105
answered Dec 23 '11 at 20:30
kennebeckennebec
79.8k2387117
79.8k2387117
2
This would not pass lint... Modifying an object that is not yours, specially a global one. This would also fail in planets with extreme tilt where daylight savings time is over 12 hours. But more realistically, if you are coding for a browser that allows changing the Date object's timezone, j1's timezone could be Australia and this's timezone could be Alaska, breaking the rounding.
– Ray Foss
Mar 10 '16 at 15:30
add a comment |
2
This would not pass lint... Modifying an object that is not yours, specially a global one. This would also fail in planets with extreme tilt where daylight savings time is over 12 hours. But more realistically, if you are coding for a browser that allows changing the Date object's timezone, j1's timezone could be Australia and this's timezone could be Alaska, breaking the rounding.
– Ray Foss
Mar 10 '16 at 15:30
2
2
This would not pass lint... Modifying an object that is not yours, specially a global one. This would also fail in planets with extreme tilt where daylight savings time is over 12 hours. But more realistically, if you are coding for a browser that allows changing the Date object's timezone, j1's timezone could be Australia and this's timezone could be Alaska, breaking the rounding.
– Ray Foss
Mar 10 '16 at 15:30
This would not pass lint... Modifying an object that is not yours, specially a global one. This would also fail in planets with extreme tilt where daylight savings time is over 12 hours. But more realistically, if you are coding for a browser that allows changing the Date object's timezone, j1's timezone could be Australia and this's timezone could be Alaska, breaking the rounding.
– Ray Foss
Mar 10 '16 at 15:30
add a comment |
Luckily this question doesn't specify if the number of the current day is required, leaving room for this answer.
Also some answers (also on other questions) had leap-year problems or used the Date-object. Although javascript's Date object
covers approximately 285616 years (100,000,000 days) on either side of January 1 1970, I was fed up with all kinds of unexpected date inconsistencies across different browsers (most notably year 0 to 99). I was also curious how to calculate it.
So I wrote a simple and above all, small algorithm to calculate the correct (Proleptic Gregorian / Astronomical / ISO 8601:2004 (clause 4.3.2.1), so year 0
exists and is a leap year and negative years are supported) day of the year based on year, month and day.
Note that in AD/BC
notation, year 0 AD/BC does not exist: instead year 1 BC
is the leap-year! IF you need to account for BC notation then simply subtract one year of the (otherwise positive) year-value first!!
I modified (for javascript) the short-circuit bitmask-modulo leapYear algorithm and came up with a magic number to do a bit-wise lookup of offsets (that excludes jan and feb, thus needing 10 * 3 bits (30 bits is less than 31 bits, so we can safely save another character on the bitshift instead of >>>
)).
Note that neither month or day may be 0
. That means that if you need this equation just for the current day (feeding it using .getMonth()
) you just need to remove the --
from --m
.
Note this assumes a valid date (although error-checking is just some characters more).
function dayNo(y,m,d){
return --m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+d;
}
<!-- some examples for the snippet -->
<input type=text value="(-)Y-M-D" onblur="
var d=this.value.match(/(-?d+)[^d]+(dd?)[^d]+(dd?)/)||;
this.nextSibling.innerHTML=' Day: ' + dayNo(+d[1], +d[2], +d[3]);
" /><span></span>
<br><hr><br>
<button onclick="
var d=new Date();
this.nextSibling.innerHTML=dayNo(d.getFullYear(), d.getMonth()+1, d.getDate()) + ' Day(s)';
">get current dayno:</button><span></span>
Here is the version with correct range-validation.
function dayNo(y,m,d){
return --m>=0 && m<12 && d>0 && d<29+(
4*(y=y&3||!(y%25)&&y&15?0:1)+15662003>>m*2&3
) && m*31-(m>1?(1054267675>>m*3-6&7)-y:0)+d;
}
<!-- some examples for the snippet -->
<input type=text value="(-)Y-M-D" onblur="
var d=this.value.match(/(-?d+)[^d]+(dd?)[^d]+(dd?)/)||;
this.nextSibling.innerHTML=' Day: ' + dayNo(+d[1], +d[2], +d[3]);
" /><span></span>
Again, one line, but I split it into 3 lines for readability (and following explanation).
The last line is identical to the function above, however the (identical) leapYear algorithm is moved to a previous short-circuit section (before the day-number calculation), because it is also needed to know how much days a month has in a given (leap) year.
The middle line calculates the correct offset number (for max number of days) for a given month in a given (leap)year using another magic number: since 31-28=3
and 3
is just 2 bits, then 12*2=24
bits, we can store all 12 months. Since addition can be faster then subtraction, we add the offset (instead of subtract it from 31
). To avoid a leap-year decision-branch for February, we modify that magic lookup-number on the fly.
That leaves us with the (pretty obvious) first line: it checks that month and date are within valid bounds and ensures us with a false
return value on range error (note that this function also should not be able to return 0, because 1 jan 0000 is still day 1.), providing easy error-checking: if(r=dayNo(/*y, m, d*/)){}
.
If used this way (where month and day may not be 0
), then one can change --m>=0 && m<12
to m>0 && --m<12
(saving another char).
The reason I typed the snippet in it's current form is that for 0-based month values, one just needs to remove the --
from --m
.
Extra:
Note, don't use this day's per month algorithm if you need just max day's per month. In that case there is a more efficient algorithm (because we only need leepYear when the month is February) I posted as answer this question: What is the best way to determine the number of days in a month with javascript?.
1
I threw it into a date function for ease of use. Granted the Date() inconsistencies are still an issue, but I'm using years 2015+ so I hope they are consistent. Poor JSHint died trying to validate your code hahah.Date.prototype.dayNo = function(){ var y = this.getFullYear(); var m = this.getMonth()+1; var d = this.getDate(); return --m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+d; };
– freshvolk
Feb 2 '15 at 20:36
Glad you liked it. This algo is fully tested for 2^31-1 years (a whopping 2147483647, that's >7500 times the range of javascript's date-object) on both sides of 0. (It might work for 2^32, but I have not tested that yet). Also, you might read my answer again: you can shave off+1
fromthis.getMonth()+1
if you remove the--
from--m
. EDIT So, I would do (for a library):Date.prototype.dayNo = function(){ var y=this.getFullYear(), m=this.getMonth(); return m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+this.getDate(); };
– GitaarLAB
Feb 2 '15 at 21:09
I actually just did that when I was looking at it! I realized I was adding one then subtracting it immediately. I think that by the time we get to2^31 - 2016
years from now js will probably be a little outdated.
– freshvolk
Feb 2 '15 at 21:12
@Freshvolk: lol, which is why I never tested it beyond 2^31:)
But until then, it will at least give predictable and consistent results haha. EDIT, just for theory's sake, using the slower conventional all-modulo algo for leapyear, the range can be extended to 2^53. The month-lookup algo is not the limiting factor.
– GitaarLAB
Feb 2 '15 at 21:27
add a comment |
Luckily this question doesn't specify if the number of the current day is required, leaving room for this answer.
Also some answers (also on other questions) had leap-year problems or used the Date-object. Although javascript's Date object
covers approximately 285616 years (100,000,000 days) on either side of January 1 1970, I was fed up with all kinds of unexpected date inconsistencies across different browsers (most notably year 0 to 99). I was also curious how to calculate it.
So I wrote a simple and above all, small algorithm to calculate the correct (Proleptic Gregorian / Astronomical / ISO 8601:2004 (clause 4.3.2.1), so year 0
exists and is a leap year and negative years are supported) day of the year based on year, month and day.
Note that in AD/BC
notation, year 0 AD/BC does not exist: instead year 1 BC
is the leap-year! IF you need to account for BC notation then simply subtract one year of the (otherwise positive) year-value first!!
I modified (for javascript) the short-circuit bitmask-modulo leapYear algorithm and came up with a magic number to do a bit-wise lookup of offsets (that excludes jan and feb, thus needing 10 * 3 bits (30 bits is less than 31 bits, so we can safely save another character on the bitshift instead of >>>
)).
Note that neither month or day may be 0
. That means that if you need this equation just for the current day (feeding it using .getMonth()
) you just need to remove the --
from --m
.
Note this assumes a valid date (although error-checking is just some characters more).
function dayNo(y,m,d){
return --m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+d;
}
<!-- some examples for the snippet -->
<input type=text value="(-)Y-M-D" onblur="
var d=this.value.match(/(-?d+)[^d]+(dd?)[^d]+(dd?)/)||;
this.nextSibling.innerHTML=' Day: ' + dayNo(+d[1], +d[2], +d[3]);
" /><span></span>
<br><hr><br>
<button onclick="
var d=new Date();
this.nextSibling.innerHTML=dayNo(d.getFullYear(), d.getMonth()+1, d.getDate()) + ' Day(s)';
">get current dayno:</button><span></span>
Here is the version with correct range-validation.
function dayNo(y,m,d){
return --m>=0 && m<12 && d>0 && d<29+(
4*(y=y&3||!(y%25)&&y&15?0:1)+15662003>>m*2&3
) && m*31-(m>1?(1054267675>>m*3-6&7)-y:0)+d;
}
<!-- some examples for the snippet -->
<input type=text value="(-)Y-M-D" onblur="
var d=this.value.match(/(-?d+)[^d]+(dd?)[^d]+(dd?)/)||;
this.nextSibling.innerHTML=' Day: ' + dayNo(+d[1], +d[2], +d[3]);
" /><span></span>
Again, one line, but I split it into 3 lines for readability (and following explanation).
The last line is identical to the function above, however the (identical) leapYear algorithm is moved to a previous short-circuit section (before the day-number calculation), because it is also needed to know how much days a month has in a given (leap) year.
The middle line calculates the correct offset number (for max number of days) for a given month in a given (leap)year using another magic number: since 31-28=3
and 3
is just 2 bits, then 12*2=24
bits, we can store all 12 months. Since addition can be faster then subtraction, we add the offset (instead of subtract it from 31
). To avoid a leap-year decision-branch for February, we modify that magic lookup-number on the fly.
That leaves us with the (pretty obvious) first line: it checks that month and date are within valid bounds and ensures us with a false
return value on range error (note that this function also should not be able to return 0, because 1 jan 0000 is still day 1.), providing easy error-checking: if(r=dayNo(/*y, m, d*/)){}
.
If used this way (where month and day may not be 0
), then one can change --m>=0 && m<12
to m>0 && --m<12
(saving another char).
The reason I typed the snippet in it's current form is that for 0-based month values, one just needs to remove the --
from --m
.
Extra:
Note, don't use this day's per month algorithm if you need just max day's per month. In that case there is a more efficient algorithm (because we only need leepYear when the month is February) I posted as answer this question: What is the best way to determine the number of days in a month with javascript?.
1
I threw it into a date function for ease of use. Granted the Date() inconsistencies are still an issue, but I'm using years 2015+ so I hope they are consistent. Poor JSHint died trying to validate your code hahah.Date.prototype.dayNo = function(){ var y = this.getFullYear(); var m = this.getMonth()+1; var d = this.getDate(); return --m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+d; };
– freshvolk
Feb 2 '15 at 20:36
Glad you liked it. This algo is fully tested for 2^31-1 years (a whopping 2147483647, that's >7500 times the range of javascript's date-object) on both sides of 0. (It might work for 2^32, but I have not tested that yet). Also, you might read my answer again: you can shave off+1
fromthis.getMonth()+1
if you remove the--
from--m
. EDIT So, I would do (for a library):Date.prototype.dayNo = function(){ var y=this.getFullYear(), m=this.getMonth(); return m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+this.getDate(); };
– GitaarLAB
Feb 2 '15 at 21:09
I actually just did that when I was looking at it! I realized I was adding one then subtracting it immediately. I think that by the time we get to2^31 - 2016
years from now js will probably be a little outdated.
– freshvolk
Feb 2 '15 at 21:12
@Freshvolk: lol, which is why I never tested it beyond 2^31:)
But until then, it will at least give predictable and consistent results haha. EDIT, just for theory's sake, using the slower conventional all-modulo algo for leapyear, the range can be extended to 2^53. The month-lookup algo is not the limiting factor.
– GitaarLAB
Feb 2 '15 at 21:27
add a comment |
Luckily this question doesn't specify if the number of the current day is required, leaving room for this answer.
Also some answers (also on other questions) had leap-year problems or used the Date-object. Although javascript's Date object
covers approximately 285616 years (100,000,000 days) on either side of January 1 1970, I was fed up with all kinds of unexpected date inconsistencies across different browsers (most notably year 0 to 99). I was also curious how to calculate it.
So I wrote a simple and above all, small algorithm to calculate the correct (Proleptic Gregorian / Astronomical / ISO 8601:2004 (clause 4.3.2.1), so year 0
exists and is a leap year and negative years are supported) day of the year based on year, month and day.
Note that in AD/BC
notation, year 0 AD/BC does not exist: instead year 1 BC
is the leap-year! IF you need to account for BC notation then simply subtract one year of the (otherwise positive) year-value first!!
I modified (for javascript) the short-circuit bitmask-modulo leapYear algorithm and came up with a magic number to do a bit-wise lookup of offsets (that excludes jan and feb, thus needing 10 * 3 bits (30 bits is less than 31 bits, so we can safely save another character on the bitshift instead of >>>
)).
Note that neither month or day may be 0
. That means that if you need this equation just for the current day (feeding it using .getMonth()
) you just need to remove the --
from --m
.
Note this assumes a valid date (although error-checking is just some characters more).
function dayNo(y,m,d){
return --m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+d;
}
<!-- some examples for the snippet -->
<input type=text value="(-)Y-M-D" onblur="
var d=this.value.match(/(-?d+)[^d]+(dd?)[^d]+(dd?)/)||;
this.nextSibling.innerHTML=' Day: ' + dayNo(+d[1], +d[2], +d[3]);
" /><span></span>
<br><hr><br>
<button onclick="
var d=new Date();
this.nextSibling.innerHTML=dayNo(d.getFullYear(), d.getMonth()+1, d.getDate()) + ' Day(s)';
">get current dayno:</button><span></span>
Here is the version with correct range-validation.
function dayNo(y,m,d){
return --m>=0 && m<12 && d>0 && d<29+(
4*(y=y&3||!(y%25)&&y&15?0:1)+15662003>>m*2&3
) && m*31-(m>1?(1054267675>>m*3-6&7)-y:0)+d;
}
<!-- some examples for the snippet -->
<input type=text value="(-)Y-M-D" onblur="
var d=this.value.match(/(-?d+)[^d]+(dd?)[^d]+(dd?)/)||;
this.nextSibling.innerHTML=' Day: ' + dayNo(+d[1], +d[2], +d[3]);
" /><span></span>
Again, one line, but I split it into 3 lines for readability (and following explanation).
The last line is identical to the function above, however the (identical) leapYear algorithm is moved to a previous short-circuit section (before the day-number calculation), because it is also needed to know how much days a month has in a given (leap) year.
The middle line calculates the correct offset number (for max number of days) for a given month in a given (leap)year using another magic number: since 31-28=3
and 3
is just 2 bits, then 12*2=24
bits, we can store all 12 months. Since addition can be faster then subtraction, we add the offset (instead of subtract it from 31
). To avoid a leap-year decision-branch for February, we modify that magic lookup-number on the fly.
That leaves us with the (pretty obvious) first line: it checks that month and date are within valid bounds and ensures us with a false
return value on range error (note that this function also should not be able to return 0, because 1 jan 0000 is still day 1.), providing easy error-checking: if(r=dayNo(/*y, m, d*/)){}
.
If used this way (where month and day may not be 0
), then one can change --m>=0 && m<12
to m>0 && --m<12
(saving another char).
The reason I typed the snippet in it's current form is that for 0-based month values, one just needs to remove the --
from --m
.
Extra:
Note, don't use this day's per month algorithm if you need just max day's per month. In that case there is a more efficient algorithm (because we only need leepYear when the month is February) I posted as answer this question: What is the best way to determine the number of days in a month with javascript?.
Luckily this question doesn't specify if the number of the current day is required, leaving room for this answer.
Also some answers (also on other questions) had leap-year problems or used the Date-object. Although javascript's Date object
covers approximately 285616 years (100,000,000 days) on either side of January 1 1970, I was fed up with all kinds of unexpected date inconsistencies across different browsers (most notably year 0 to 99). I was also curious how to calculate it.
So I wrote a simple and above all, small algorithm to calculate the correct (Proleptic Gregorian / Astronomical / ISO 8601:2004 (clause 4.3.2.1), so year 0
exists and is a leap year and negative years are supported) day of the year based on year, month and day.
Note that in AD/BC
notation, year 0 AD/BC does not exist: instead year 1 BC
is the leap-year! IF you need to account for BC notation then simply subtract one year of the (otherwise positive) year-value first!!
I modified (for javascript) the short-circuit bitmask-modulo leapYear algorithm and came up with a magic number to do a bit-wise lookup of offsets (that excludes jan and feb, thus needing 10 * 3 bits (30 bits is less than 31 bits, so we can safely save another character on the bitshift instead of >>>
)).
Note that neither month or day may be 0
. That means that if you need this equation just for the current day (feeding it using .getMonth()
) you just need to remove the --
from --m
.
Note this assumes a valid date (although error-checking is just some characters more).
function dayNo(y,m,d){
return --m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+d;
}
<!-- some examples for the snippet -->
<input type=text value="(-)Y-M-D" onblur="
var d=this.value.match(/(-?d+)[^d]+(dd?)[^d]+(dd?)/)||;
this.nextSibling.innerHTML=' Day: ' + dayNo(+d[1], +d[2], +d[3]);
" /><span></span>
<br><hr><br>
<button onclick="
var d=new Date();
this.nextSibling.innerHTML=dayNo(d.getFullYear(), d.getMonth()+1, d.getDate()) + ' Day(s)';
">get current dayno:</button><span></span>
Here is the version with correct range-validation.
function dayNo(y,m,d){
return --m>=0 && m<12 && d>0 && d<29+(
4*(y=y&3||!(y%25)&&y&15?0:1)+15662003>>m*2&3
) && m*31-(m>1?(1054267675>>m*3-6&7)-y:0)+d;
}
<!-- some examples for the snippet -->
<input type=text value="(-)Y-M-D" onblur="
var d=this.value.match(/(-?d+)[^d]+(dd?)[^d]+(dd?)/)||;
this.nextSibling.innerHTML=' Day: ' + dayNo(+d[1], +d[2], +d[3]);
" /><span></span>
Again, one line, but I split it into 3 lines for readability (and following explanation).
The last line is identical to the function above, however the (identical) leapYear algorithm is moved to a previous short-circuit section (before the day-number calculation), because it is also needed to know how much days a month has in a given (leap) year.
The middle line calculates the correct offset number (for max number of days) for a given month in a given (leap)year using another magic number: since 31-28=3
and 3
is just 2 bits, then 12*2=24
bits, we can store all 12 months. Since addition can be faster then subtraction, we add the offset (instead of subtract it from 31
). To avoid a leap-year decision-branch for February, we modify that magic lookup-number on the fly.
That leaves us with the (pretty obvious) first line: it checks that month and date are within valid bounds and ensures us with a false
return value on range error (note that this function also should not be able to return 0, because 1 jan 0000 is still day 1.), providing easy error-checking: if(r=dayNo(/*y, m, d*/)){}
.
If used this way (where month and day may not be 0
), then one can change --m>=0 && m<12
to m>0 && --m<12
(saving another char).
The reason I typed the snippet in it's current form is that for 0-based month values, one just needs to remove the --
from --m
.
Extra:
Note, don't use this day's per month algorithm if you need just max day's per month. In that case there is a more efficient algorithm (because we only need leepYear when the month is February) I posted as answer this question: What is the best way to determine the number of days in a month with javascript?.
function dayNo(y,m,d){
return --m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+d;
}
<!-- some examples for the snippet -->
<input type=text value="(-)Y-M-D" onblur="
var d=this.value.match(/(-?d+)[^d]+(dd?)[^d]+(dd?)/)||;
this.nextSibling.innerHTML=' Day: ' + dayNo(+d[1], +d[2], +d[3]);
" /><span></span>
<br><hr><br>
<button onclick="
var d=new Date();
this.nextSibling.innerHTML=dayNo(d.getFullYear(), d.getMonth()+1, d.getDate()) + ' Day(s)';
">get current dayno:</button><span></span>
function dayNo(y,m,d){
return --m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+d;
}
<!-- some examples for the snippet -->
<input type=text value="(-)Y-M-D" onblur="
var d=this.value.match(/(-?d+)[^d]+(dd?)[^d]+(dd?)/)||;
this.nextSibling.innerHTML=' Day: ' + dayNo(+d[1], +d[2], +d[3]);
" /><span></span>
<br><hr><br>
<button onclick="
var d=new Date();
this.nextSibling.innerHTML=dayNo(d.getFullYear(), d.getMonth()+1, d.getDate()) + ' Day(s)';
">get current dayno:</button><span></span>
function dayNo(y,m,d){
return --m>=0 && m<12 && d>0 && d<29+(
4*(y=y&3||!(y%25)&&y&15?0:1)+15662003>>m*2&3
) && m*31-(m>1?(1054267675>>m*3-6&7)-y:0)+d;
}
<!-- some examples for the snippet -->
<input type=text value="(-)Y-M-D" onblur="
var d=this.value.match(/(-?d+)[^d]+(dd?)[^d]+(dd?)/)||;
this.nextSibling.innerHTML=' Day: ' + dayNo(+d[1], +d[2], +d[3]);
" /><span></span>
function dayNo(y,m,d){
return --m>=0 && m<12 && d>0 && d<29+(
4*(y=y&3||!(y%25)&&y&15?0:1)+15662003>>m*2&3
) && m*31-(m>1?(1054267675>>m*3-6&7)-y:0)+d;
}
<!-- some examples for the snippet -->
<input type=text value="(-)Y-M-D" onblur="
var d=this.value.match(/(-?d+)[^d]+(dd?)[^d]+(dd?)/)||;
this.nextSibling.innerHTML=' Day: ' + dayNo(+d[1], +d[2], +d[3]);
" /><span></span>
edited May 23 '17 at 12:26
Community♦
11
11
answered Jan 6 '15 at 0:55
GitaarLABGitaarLAB
11.7k74470
11.7k74470
1
I threw it into a date function for ease of use. Granted the Date() inconsistencies are still an issue, but I'm using years 2015+ so I hope they are consistent. Poor JSHint died trying to validate your code hahah.Date.prototype.dayNo = function(){ var y = this.getFullYear(); var m = this.getMonth()+1; var d = this.getDate(); return --m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+d; };
– freshvolk
Feb 2 '15 at 20:36
Glad you liked it. This algo is fully tested for 2^31-1 years (a whopping 2147483647, that's >7500 times the range of javascript's date-object) on both sides of 0. (It might work for 2^32, but I have not tested that yet). Also, you might read my answer again: you can shave off+1
fromthis.getMonth()+1
if you remove the--
from--m
. EDIT So, I would do (for a library):Date.prototype.dayNo = function(){ var y=this.getFullYear(), m=this.getMonth(); return m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+this.getDate(); };
– GitaarLAB
Feb 2 '15 at 21:09
I actually just did that when I was looking at it! I realized I was adding one then subtracting it immediately. I think that by the time we get to2^31 - 2016
years from now js will probably be a little outdated.
– freshvolk
Feb 2 '15 at 21:12
@Freshvolk: lol, which is why I never tested it beyond 2^31:)
But until then, it will at least give predictable and consistent results haha. EDIT, just for theory's sake, using the slower conventional all-modulo algo for leapyear, the range can be extended to 2^53. The month-lookup algo is not the limiting factor.
– GitaarLAB
Feb 2 '15 at 21:27
add a comment |
1
I threw it into a date function for ease of use. Granted the Date() inconsistencies are still an issue, but I'm using years 2015+ so I hope they are consistent. Poor JSHint died trying to validate your code hahah.Date.prototype.dayNo = function(){ var y = this.getFullYear(); var m = this.getMonth()+1; var d = this.getDate(); return --m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+d; };
– freshvolk
Feb 2 '15 at 20:36
Glad you liked it. This algo is fully tested for 2^31-1 years (a whopping 2147483647, that's >7500 times the range of javascript's date-object) on both sides of 0. (It might work for 2^32, but I have not tested that yet). Also, you might read my answer again: you can shave off+1
fromthis.getMonth()+1
if you remove the--
from--m
. EDIT So, I would do (for a library):Date.prototype.dayNo = function(){ var y=this.getFullYear(), m=this.getMonth(); return m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+this.getDate(); };
– GitaarLAB
Feb 2 '15 at 21:09
I actually just did that when I was looking at it! I realized I was adding one then subtracting it immediately. I think that by the time we get to2^31 - 2016
years from now js will probably be a little outdated.
– freshvolk
Feb 2 '15 at 21:12
@Freshvolk: lol, which is why I never tested it beyond 2^31:)
But until then, it will at least give predictable and consistent results haha. EDIT, just for theory's sake, using the slower conventional all-modulo algo for leapyear, the range can be extended to 2^53. The month-lookup algo is not the limiting factor.
– GitaarLAB
Feb 2 '15 at 21:27
1
1
I threw it into a date function for ease of use. Granted the Date() inconsistencies are still an issue, but I'm using years 2015+ so I hope they are consistent. Poor JSHint died trying to validate your code hahah.
Date.prototype.dayNo = function(){ var y = this.getFullYear(); var m = this.getMonth()+1; var d = this.getDate(); return --m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+d; };
– freshvolk
Feb 2 '15 at 20:36
I threw it into a date function for ease of use. Granted the Date() inconsistencies are still an issue, but I'm using years 2015+ so I hope they are consistent. Poor JSHint died trying to validate your code hahah.
Date.prototype.dayNo = function(){ var y = this.getFullYear(); var m = this.getMonth()+1; var d = this.getDate(); return --m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+d; };
– freshvolk
Feb 2 '15 at 20:36
Glad you liked it. This algo is fully tested for 2^31-1 years (a whopping 2147483647, that's >7500 times the range of javascript's date-object) on both sides of 0. (It might work for 2^32, but I have not tested that yet). Also, you might read my answer again: you can shave off
+1
from this.getMonth()+1
if you remove the --
from --m
. EDIT So, I would do (for a library): Date.prototype.dayNo = function(){ var y=this.getFullYear(), m=this.getMonth(); return m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+this.getDate(); };
– GitaarLAB
Feb 2 '15 at 21:09
Glad you liked it. This algo is fully tested for 2^31-1 years (a whopping 2147483647, that's >7500 times the range of javascript's date-object) on both sides of 0. (It might work for 2^32, but I have not tested that yet). Also, you might read my answer again: you can shave off
+1
from this.getMonth()+1
if you remove the --
from --m
. EDIT So, I would do (for a library): Date.prototype.dayNo = function(){ var y=this.getFullYear(), m=this.getMonth(); return m*31-(m>1?(1054267675>>m*3-6&7)-(y&3||!(y%25)&&y&15?0:1):0)+this.getDate(); };
– GitaarLAB
Feb 2 '15 at 21:09
I actually just did that when I was looking at it! I realized I was adding one then subtracting it immediately. I think that by the time we get to
2^31 - 2016
years from now js will probably be a little outdated.– freshvolk
Feb 2 '15 at 21:12
I actually just did that when I was looking at it! I realized I was adding one then subtracting it immediately. I think that by the time we get to
2^31 - 2016
years from now js will probably be a little outdated.– freshvolk
Feb 2 '15 at 21:12
@Freshvolk: lol, which is why I never tested it beyond 2^31
:)
But until then, it will at least give predictable and consistent results haha. EDIT, just for theory's sake, using the slower conventional all-modulo algo for leapyear, the range can be extended to 2^53. The month-lookup algo is not the limiting factor.– GitaarLAB
Feb 2 '15 at 21:27
@Freshvolk: lol, which is why I never tested it beyond 2^31
:)
But until then, it will at least give predictable and consistent results haha. EDIT, just for theory's sake, using the slower conventional all-modulo algo for leapyear, the range can be extended to 2^53. The month-lookup algo is not the limiting factor.– GitaarLAB
Feb 2 '15 at 21:27
add a comment |
Well, if I understand you correctly, you want 366 on a leap year, 365 otherwise, right? A year is a leap year if it's evenly divisible by 4 but not by 100 unless it's also divisible by 400:
function daysInYear(year) {
if(year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
// Leap year
return 366;
} else {
// Not a leap year
return 365;
}
}
Edit after update:
In that case, I don't think there's a built-in method; you'll need to do this:
function daysInFebruary(year) {
if(year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
// Leap year
return 29;
} else {
// Not a leap year
return 28;
}
}
function dateToDay(date) {
var feb = daysInFebruary(date.getFullYear());
var aggregateMonths = [0, // January
31, // February
31 + feb, // March
31 + feb + 31, // April
31 + feb + 31 + 30, // May
31 + feb + 31 + 30 + 31, // June
31 + feb + 31 + 30 + 31 + 30, // July
31 + feb + 31 + 30 + 31 + 30 + 31, // August
31 + feb + 31 + 30 + 31 + 30 + 31 + 31, // September
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30, // October
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, // November
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, // December
];
return aggregateMonths[date.getMonth()] + date.getDate();
}
(Yes, I actually did that without copying or pasting. If there's an easy way I'll be mad)
My easy way is just using magic number 1054267675 and call it a day, I'm way to lazy to type all that:)
– GitaarLAB
Jan 6 '15 at 2:25
add a comment |
Well, if I understand you correctly, you want 366 on a leap year, 365 otherwise, right? A year is a leap year if it's evenly divisible by 4 but not by 100 unless it's also divisible by 400:
function daysInYear(year) {
if(year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
// Leap year
return 366;
} else {
// Not a leap year
return 365;
}
}
Edit after update:
In that case, I don't think there's a built-in method; you'll need to do this:
function daysInFebruary(year) {
if(year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
// Leap year
return 29;
} else {
// Not a leap year
return 28;
}
}
function dateToDay(date) {
var feb = daysInFebruary(date.getFullYear());
var aggregateMonths = [0, // January
31, // February
31 + feb, // March
31 + feb + 31, // April
31 + feb + 31 + 30, // May
31 + feb + 31 + 30 + 31, // June
31 + feb + 31 + 30 + 31 + 30, // July
31 + feb + 31 + 30 + 31 + 30 + 31, // August
31 + feb + 31 + 30 + 31 + 30 + 31 + 31, // September
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30, // October
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, // November
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, // December
];
return aggregateMonths[date.getMonth()] + date.getDate();
}
(Yes, I actually did that without copying or pasting. If there's an easy way I'll be mad)
My easy way is just using magic number 1054267675 and call it a day, I'm way to lazy to type all that:)
– GitaarLAB
Jan 6 '15 at 2:25
add a comment |
Well, if I understand you correctly, you want 366 on a leap year, 365 otherwise, right? A year is a leap year if it's evenly divisible by 4 but not by 100 unless it's also divisible by 400:
function daysInYear(year) {
if(year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
// Leap year
return 366;
} else {
// Not a leap year
return 365;
}
}
Edit after update:
In that case, I don't think there's a built-in method; you'll need to do this:
function daysInFebruary(year) {
if(year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
// Leap year
return 29;
} else {
// Not a leap year
return 28;
}
}
function dateToDay(date) {
var feb = daysInFebruary(date.getFullYear());
var aggregateMonths = [0, // January
31, // February
31 + feb, // March
31 + feb + 31, // April
31 + feb + 31 + 30, // May
31 + feb + 31 + 30 + 31, // June
31 + feb + 31 + 30 + 31 + 30, // July
31 + feb + 31 + 30 + 31 + 30 + 31, // August
31 + feb + 31 + 30 + 31 + 30 + 31 + 31, // September
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30, // October
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, // November
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, // December
];
return aggregateMonths[date.getMonth()] + date.getDate();
}
(Yes, I actually did that without copying or pasting. If there's an easy way I'll be mad)
Well, if I understand you correctly, you want 366 on a leap year, 365 otherwise, right? A year is a leap year if it's evenly divisible by 4 but not by 100 unless it's also divisible by 400:
function daysInYear(year) {
if(year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
// Leap year
return 366;
} else {
// Not a leap year
return 365;
}
}
Edit after update:
In that case, I don't think there's a built-in method; you'll need to do this:
function daysInFebruary(year) {
if(year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
// Leap year
return 29;
} else {
// Not a leap year
return 28;
}
}
function dateToDay(date) {
var feb = daysInFebruary(date.getFullYear());
var aggregateMonths = [0, // January
31, // February
31 + feb, // March
31 + feb + 31, // April
31 + feb + 31 + 30, // May
31 + feb + 31 + 30 + 31, // June
31 + feb + 31 + 30 + 31 + 30, // July
31 + feb + 31 + 30 + 31 + 30 + 31, // August
31 + feb + 31 + 30 + 31 + 30 + 31 + 31, // September
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30, // October
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, // November
31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, // December
];
return aggregateMonths[date.getMonth()] + date.getDate();
}
(Yes, I actually did that without copying or pasting. If there's an easy way I'll be mad)
edited Dec 23 '11 at 19:39
answered Dec 23 '11 at 19:29
Ry-♦Ry-
170k40347363
170k40347363
My easy way is just using magic number 1054267675 and call it a day, I'm way to lazy to type all that:)
– GitaarLAB
Jan 6 '15 at 2:25
add a comment |
My easy way is just using magic number 1054267675 and call it a day, I'm way to lazy to type all that:)
– GitaarLAB
Jan 6 '15 at 2:25
My easy way is just using magic number 1054267675 and call it a day, I'm way to lazy to type all that
:)
– GitaarLAB
Jan 6 '15 at 2:25
My easy way is just using magic number 1054267675 and call it a day, I'm way to lazy to type all that
:)
– GitaarLAB
Jan 6 '15 at 2:25
add a comment |
This is a simple way to find the current day in the year, and it should account for leap years without a problem:
Javascript:
Math.round((new Date().setHours(23) - new Date(new Date().getYear()+1900, 0, 1, 0, 0, 0))/1000/60/60/24);
Javascript in Google Apps Script:
Math.round((new Date().setHours(23) - new Date(new Date().getYear(), 0, 1, 0, 0, 0))/1000/60/60/24);
The primary action of this code is to find the number of milliseconds that have elapsed in the current year and then convert this number into days. The number of milliseconds that have elapsed in the current year can be found by subtracting the number of milliseconds of the first second of the first day of the current year, which is obtained with new Date(new Date().getYear()+1900, 0, 1, 0, 0, 0)
(Javascript) or new Date(new Date().getYear(), 0, 1, 0, 0, 0)
(Google Apps Script), from the milliseconds of the 23rd hour of the current day, which was found with new Date().setHours(23)
. The purpose of setting the current date to the 23rd hour is to ensure that the day of year is rounded correctly by Math.round()
.
Once you have the number of milliseconds of the current year, then you can convert this time into days by dividing by 1000 to convert milliseconds to seconds, then dividing by 60 to convert seconds to minutes, then dividing by 60 to convert minutes to hours, and finally dividing by 24 to convert hours to days.
Note: This post was edited to account for differences between JavaScript and JavaScript implemented in Google Apps Script. Also, more context was added for the answer.
Can you please explain a little bit more than just saying it's working?
– Stephen Reindl
Mar 7 '15 at 19:51
Yes! I am sorry for not providing more detail in the first place. The expression inside the "Math.round" statement finds the number of milliseconds in the current year by subtracting the first day of the year from the milliseconds of the last hour of the current day of the year. The number of milliseconds is then divided by 1000 to convert to seconds, 60 to convert to minutes, 60 to convert to hours, and 24 to convert to days. The expression is wrapped within a "Math.round()" function so that it rounds to an integer for the day of year.
– Liz Page-Gould
Mar 7 '15 at 20:13
I should also add that this solution is essentially the same as the "accepted" solution, except it does everything in one line of code.
– Liz Page-Gould
Mar 7 '15 at 20:45
How does this account for daylight savings time? Wouldn't that throw it off a bit?
– Dan Oswalt
Mar 7 '15 at 22:34
No, daylight savings time would not affect this calculation, because daylight savings time has the resolution of hours and this code is calculating the number of days in the year. Moreover, because I used the.setHours
method on the first date object and specified the time of day with the second date object, a change of 1 hour from daylight savings time will not affect the times of the Date objects used in this calculation.
– Liz Page-Gould
Mar 8 '15 at 14:45
add a comment |
This is a simple way to find the current day in the year, and it should account for leap years without a problem:
Javascript:
Math.round((new Date().setHours(23) - new Date(new Date().getYear()+1900, 0, 1, 0, 0, 0))/1000/60/60/24);
Javascript in Google Apps Script:
Math.round((new Date().setHours(23) - new Date(new Date().getYear(), 0, 1, 0, 0, 0))/1000/60/60/24);
The primary action of this code is to find the number of milliseconds that have elapsed in the current year and then convert this number into days. The number of milliseconds that have elapsed in the current year can be found by subtracting the number of milliseconds of the first second of the first day of the current year, which is obtained with new Date(new Date().getYear()+1900, 0, 1, 0, 0, 0)
(Javascript) or new Date(new Date().getYear(), 0, 1, 0, 0, 0)
(Google Apps Script), from the milliseconds of the 23rd hour of the current day, which was found with new Date().setHours(23)
. The purpose of setting the current date to the 23rd hour is to ensure that the day of year is rounded correctly by Math.round()
.
Once you have the number of milliseconds of the current year, then you can convert this time into days by dividing by 1000 to convert milliseconds to seconds, then dividing by 60 to convert seconds to minutes, then dividing by 60 to convert minutes to hours, and finally dividing by 24 to convert hours to days.
Note: This post was edited to account for differences between JavaScript and JavaScript implemented in Google Apps Script. Also, more context was added for the answer.
Can you please explain a little bit more than just saying it's working?
– Stephen Reindl
Mar 7 '15 at 19:51
Yes! I am sorry for not providing more detail in the first place. The expression inside the "Math.round" statement finds the number of milliseconds in the current year by subtracting the first day of the year from the milliseconds of the last hour of the current day of the year. The number of milliseconds is then divided by 1000 to convert to seconds, 60 to convert to minutes, 60 to convert to hours, and 24 to convert to days. The expression is wrapped within a "Math.round()" function so that it rounds to an integer for the day of year.
– Liz Page-Gould
Mar 7 '15 at 20:13
I should also add that this solution is essentially the same as the "accepted" solution, except it does everything in one line of code.
– Liz Page-Gould
Mar 7 '15 at 20:45
How does this account for daylight savings time? Wouldn't that throw it off a bit?
– Dan Oswalt
Mar 7 '15 at 22:34
No, daylight savings time would not affect this calculation, because daylight savings time has the resolution of hours and this code is calculating the number of days in the year. Moreover, because I used the.setHours
method on the first date object and specified the time of day with the second date object, a change of 1 hour from daylight savings time will not affect the times of the Date objects used in this calculation.
– Liz Page-Gould
Mar 8 '15 at 14:45
add a comment |
This is a simple way to find the current day in the year, and it should account for leap years without a problem:
Javascript:
Math.round((new Date().setHours(23) - new Date(new Date().getYear()+1900, 0, 1, 0, 0, 0))/1000/60/60/24);
Javascript in Google Apps Script:
Math.round((new Date().setHours(23) - new Date(new Date().getYear(), 0, 1, 0, 0, 0))/1000/60/60/24);
The primary action of this code is to find the number of milliseconds that have elapsed in the current year and then convert this number into days. The number of milliseconds that have elapsed in the current year can be found by subtracting the number of milliseconds of the first second of the first day of the current year, which is obtained with new Date(new Date().getYear()+1900, 0, 1, 0, 0, 0)
(Javascript) or new Date(new Date().getYear(), 0, 1, 0, 0, 0)
(Google Apps Script), from the milliseconds of the 23rd hour of the current day, which was found with new Date().setHours(23)
. The purpose of setting the current date to the 23rd hour is to ensure that the day of year is rounded correctly by Math.round()
.
Once you have the number of milliseconds of the current year, then you can convert this time into days by dividing by 1000 to convert milliseconds to seconds, then dividing by 60 to convert seconds to minutes, then dividing by 60 to convert minutes to hours, and finally dividing by 24 to convert hours to days.
Note: This post was edited to account for differences between JavaScript and JavaScript implemented in Google Apps Script. Also, more context was added for the answer.
This is a simple way to find the current day in the year, and it should account for leap years without a problem:
Javascript:
Math.round((new Date().setHours(23) - new Date(new Date().getYear()+1900, 0, 1, 0, 0, 0))/1000/60/60/24);
Javascript in Google Apps Script:
Math.round((new Date().setHours(23) - new Date(new Date().getYear(), 0, 1, 0, 0, 0))/1000/60/60/24);
The primary action of this code is to find the number of milliseconds that have elapsed in the current year and then convert this number into days. The number of milliseconds that have elapsed in the current year can be found by subtracting the number of milliseconds of the first second of the first day of the current year, which is obtained with new Date(new Date().getYear()+1900, 0, 1, 0, 0, 0)
(Javascript) or new Date(new Date().getYear(), 0, 1, 0, 0, 0)
(Google Apps Script), from the milliseconds of the 23rd hour of the current day, which was found with new Date().setHours(23)
. The purpose of setting the current date to the 23rd hour is to ensure that the day of year is rounded correctly by Math.round()
.
Once you have the number of milliseconds of the current year, then you can convert this time into days by dividing by 1000 to convert milliseconds to seconds, then dividing by 60 to convert seconds to minutes, then dividing by 60 to convert minutes to hours, and finally dividing by 24 to convert hours to days.
Note: This post was edited to account for differences between JavaScript and JavaScript implemented in Google Apps Script. Also, more context was added for the answer.
edited Mar 8 '15 at 15:57
answered Mar 7 '15 at 19:45
Liz Page-GouldLiz Page-Gould
15615
15615
Can you please explain a little bit more than just saying it's working?
– Stephen Reindl
Mar 7 '15 at 19:51
Yes! I am sorry for not providing more detail in the first place. The expression inside the "Math.round" statement finds the number of milliseconds in the current year by subtracting the first day of the year from the milliseconds of the last hour of the current day of the year. The number of milliseconds is then divided by 1000 to convert to seconds, 60 to convert to minutes, 60 to convert to hours, and 24 to convert to days. The expression is wrapped within a "Math.round()" function so that it rounds to an integer for the day of year.
– Liz Page-Gould
Mar 7 '15 at 20:13
I should also add that this solution is essentially the same as the "accepted" solution, except it does everything in one line of code.
– Liz Page-Gould
Mar 7 '15 at 20:45
How does this account for daylight savings time? Wouldn't that throw it off a bit?
– Dan Oswalt
Mar 7 '15 at 22:34
No, daylight savings time would not affect this calculation, because daylight savings time has the resolution of hours and this code is calculating the number of days in the year. Moreover, because I used the.setHours
method on the first date object and specified the time of day with the second date object, a change of 1 hour from daylight savings time will not affect the times of the Date objects used in this calculation.
– Liz Page-Gould
Mar 8 '15 at 14:45
add a comment |
Can you please explain a little bit more than just saying it's working?
– Stephen Reindl
Mar 7 '15 at 19:51
Yes! I am sorry for not providing more detail in the first place. The expression inside the "Math.round" statement finds the number of milliseconds in the current year by subtracting the first day of the year from the milliseconds of the last hour of the current day of the year. The number of milliseconds is then divided by 1000 to convert to seconds, 60 to convert to minutes, 60 to convert to hours, and 24 to convert to days. The expression is wrapped within a "Math.round()" function so that it rounds to an integer for the day of year.
– Liz Page-Gould
Mar 7 '15 at 20:13
I should also add that this solution is essentially the same as the "accepted" solution, except it does everything in one line of code.
– Liz Page-Gould
Mar 7 '15 at 20:45
How does this account for daylight savings time? Wouldn't that throw it off a bit?
– Dan Oswalt
Mar 7 '15 at 22:34
No, daylight savings time would not affect this calculation, because daylight savings time has the resolution of hours and this code is calculating the number of days in the year. Moreover, because I used the.setHours
method on the first date object and specified the time of day with the second date object, a change of 1 hour from daylight savings time will not affect the times of the Date objects used in this calculation.
– Liz Page-Gould
Mar 8 '15 at 14:45
Can you please explain a little bit more than just saying it's working?
– Stephen Reindl
Mar 7 '15 at 19:51
Can you please explain a little bit more than just saying it's working?
– Stephen Reindl
Mar 7 '15 at 19:51
Yes! I am sorry for not providing more detail in the first place. The expression inside the "Math.round" statement finds the number of milliseconds in the current year by subtracting the first day of the year from the milliseconds of the last hour of the current day of the year. The number of milliseconds is then divided by 1000 to convert to seconds, 60 to convert to minutes, 60 to convert to hours, and 24 to convert to days. The expression is wrapped within a "Math.round()" function so that it rounds to an integer for the day of year.
– Liz Page-Gould
Mar 7 '15 at 20:13
Yes! I am sorry for not providing more detail in the first place. The expression inside the "Math.round" statement finds the number of milliseconds in the current year by subtracting the first day of the year from the milliseconds of the last hour of the current day of the year. The number of milliseconds is then divided by 1000 to convert to seconds, 60 to convert to minutes, 60 to convert to hours, and 24 to convert to days. The expression is wrapped within a "Math.round()" function so that it rounds to an integer for the day of year.
– Liz Page-Gould
Mar 7 '15 at 20:13
I should also add that this solution is essentially the same as the "accepted" solution, except it does everything in one line of code.
– Liz Page-Gould
Mar 7 '15 at 20:45
I should also add that this solution is essentially the same as the "accepted" solution, except it does everything in one line of code.
– Liz Page-Gould
Mar 7 '15 at 20:45
How does this account for daylight savings time? Wouldn't that throw it off a bit?
– Dan Oswalt
Mar 7 '15 at 22:34
How does this account for daylight savings time? Wouldn't that throw it off a bit?
– Dan Oswalt
Mar 7 '15 at 22:34
No, daylight savings time would not affect this calculation, because daylight savings time has the resolution of hours and this code is calculating the number of days in the year. Moreover, because I used the
.setHours
method on the first date object and specified the time of day with the second date object, a change of 1 hour from daylight savings time will not affect the times of the Date objects used in this calculation.– Liz Page-Gould
Mar 8 '15 at 14:45
No, daylight savings time would not affect this calculation, because daylight savings time has the resolution of hours and this code is calculating the number of days in the year. Moreover, because I used the
.setHours
method on the first date object and specified the time of day with the second date object, a change of 1 hour from daylight savings time will not affect the times of the Date objects used in this calculation.– Liz Page-Gould
Mar 8 '15 at 14:45
add a comment |
This method takes into account timezone issue and daylight saving time
function dayofyear(d) { // d is a Date object
var yn = d.getFullYear();
var mn = d.getMonth();
var dn = d.getDate();
var d1 = new Date(yn,0,1,12,0,0); // noon on Jan. 1
var d2 = new Date(yn,mn,dn,12,0,0); // noon on input date
var ddiff = Math.round((d2-d1)/864e5);
return ddiff+1;
}
(took from here)
See also this fiddle
This is a bit inefficient, but it works perfectly. You could just get time the objects and setDate change the date to day 1. Both of which are perfectly supported. The idea is you wont be moving around timezones. This also assumes that daylight savings time is less than 12 hours... which is a safe assumption on earth.
– Ray Foss
Mar 10 '16 at 15:55
add a comment |
This method takes into account timezone issue and daylight saving time
function dayofyear(d) { // d is a Date object
var yn = d.getFullYear();
var mn = d.getMonth();
var dn = d.getDate();
var d1 = new Date(yn,0,1,12,0,0); // noon on Jan. 1
var d2 = new Date(yn,mn,dn,12,0,0); // noon on input date
var ddiff = Math.round((d2-d1)/864e5);
return ddiff+1;
}
(took from here)
See also this fiddle
This is a bit inefficient, but it works perfectly. You could just get time the objects and setDate change the date to day 1. Both of which are perfectly supported. The idea is you wont be moving around timezones. This also assumes that daylight savings time is less than 12 hours... which is a safe assumption on earth.
– Ray Foss
Mar 10 '16 at 15:55
add a comment |
This method takes into account timezone issue and daylight saving time
function dayofyear(d) { // d is a Date object
var yn = d.getFullYear();
var mn = d.getMonth();
var dn = d.getDate();
var d1 = new Date(yn,0,1,12,0,0); // noon on Jan. 1
var d2 = new Date(yn,mn,dn,12,0,0); // noon on input date
var ddiff = Math.round((d2-d1)/864e5);
return ddiff+1;
}
(took from here)
See also this fiddle
This method takes into account timezone issue and daylight saving time
function dayofyear(d) { // d is a Date object
var yn = d.getFullYear();
var mn = d.getMonth();
var dn = d.getDate();
var d1 = new Date(yn,0,1,12,0,0); // noon on Jan. 1
var d2 = new Date(yn,mn,dn,12,0,0); // noon on input date
var ddiff = Math.round((d2-d1)/864e5);
return ddiff+1;
}
(took from here)
See also this fiddle
edited Oct 15 '14 at 20:19
answered Oct 15 '14 at 20:13
Andriy F.Andriy F.
1,9181720
1,9181720
This is a bit inefficient, but it works perfectly. You could just get time the objects and setDate change the date to day 1. Both of which are perfectly supported. The idea is you wont be moving around timezones. This also assumes that daylight savings time is less than 12 hours... which is a safe assumption on earth.
– Ray Foss
Mar 10 '16 at 15:55
add a comment |
This is a bit inefficient, but it works perfectly. You could just get time the objects and setDate change the date to day 1. Both of which are perfectly supported. The idea is you wont be moving around timezones. This also assumes that daylight savings time is less than 12 hours... which is a safe assumption on earth.
– Ray Foss
Mar 10 '16 at 15:55
This is a bit inefficient, but it works perfectly. You could just get time the objects and setDate change the date to day 1. Both of which are perfectly supported. The idea is you wont be moving around timezones. This also assumes that daylight savings time is less than 12 hours... which is a safe assumption on earth.
– Ray Foss
Mar 10 '16 at 15:55
This is a bit inefficient, but it works perfectly. You could just get time the objects and setDate change the date to day 1. Both of which are perfectly supported. The idea is you wont be moving around timezones. This also assumes that daylight savings time is less than 12 hours... which is a safe assumption on earth.
– Ray Foss
Mar 10 '16 at 15:55
add a comment |
If you don't want to re-invent the wheel, you can use the excellent date-fns (node.js) library:
var getDayOfYear = require('date-fns/get_day_of_year')
var dayOfYear = getDayOfYear(new Date(2017, 1, 1)) // 1st february => 32
add a comment |
If you don't want to re-invent the wheel, you can use the excellent date-fns (node.js) library:
var getDayOfYear = require('date-fns/get_day_of_year')
var dayOfYear = getDayOfYear(new Date(2017, 1, 1)) // 1st february => 32
add a comment |
If you don't want to re-invent the wheel, you can use the excellent date-fns (node.js) library:
var getDayOfYear = require('date-fns/get_day_of_year')
var dayOfYear = getDayOfYear(new Date(2017, 1, 1)) // 1st february => 32
If you don't want to re-invent the wheel, you can use the excellent date-fns (node.js) library:
var getDayOfYear = require('date-fns/get_day_of_year')
var dayOfYear = getDayOfYear(new Date(2017, 1, 1)) // 1st february => 32
answered Jul 2 '17 at 17:45
Amaury LietAmaury Liet
2,9601512
2,9601512
add a comment |
add a comment |
Math.round((new Date().setHours(23) - new Date(new Date().getFullYear(), 0, 1, 0, 0, 0))/1000/86400);
further optimizes the answer.
Moreover, by changing setHours(23) or the last-but-two zero later on to another value may provide day-of-year related to another timezone.
For example, to retrieve from Europe a resource located in America.
add a comment |
Math.round((new Date().setHours(23) - new Date(new Date().getFullYear(), 0, 1, 0, 0, 0))/1000/86400);
further optimizes the answer.
Moreover, by changing setHours(23) or the last-but-two zero later on to another value may provide day-of-year related to another timezone.
For example, to retrieve from Europe a resource located in America.
add a comment |
Math.round((new Date().setHours(23) - new Date(new Date().getFullYear(), 0, 1, 0, 0, 0))/1000/86400);
further optimizes the answer.
Moreover, by changing setHours(23) or the last-but-two zero later on to another value may provide day-of-year related to another timezone.
For example, to retrieve from Europe a resource located in America.
Math.round((new Date().setHours(23) - new Date(new Date().getFullYear(), 0, 1, 0, 0, 0))/1000/86400);
further optimizes the answer.
Moreover, by changing setHours(23) or the last-but-two zero later on to another value may provide day-of-year related to another timezone.
For example, to retrieve from Europe a resource located in America.
answered Aug 2 '15 at 10:03
Massimo RoscioMassimo Roscio
211
211
add a comment |
add a comment |
I think this is more straightforward:
var date365 = 0;
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var currentMonth = currentDate.getMonth();
var currentDay = currentDate.getDate();
var monthLength = [31,28,31,30,31,30,31,31,30,31,30,31];
var leapYear = new Date(currentYear, 1, 29);
if (leapYear.getDate() == 29) { // If it's a leap year, changes 28 to 29
monthLength[1] = 29;
}
for ( i=0; i < currentMonth; i++ ) {
date365 = date365 + monthLength[i];
}
date365 = date365 + currentDay; // Done!
add a comment |
I think this is more straightforward:
var date365 = 0;
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var currentMonth = currentDate.getMonth();
var currentDay = currentDate.getDate();
var monthLength = [31,28,31,30,31,30,31,31,30,31,30,31];
var leapYear = new Date(currentYear, 1, 29);
if (leapYear.getDate() == 29) { // If it's a leap year, changes 28 to 29
monthLength[1] = 29;
}
for ( i=0; i < currentMonth; i++ ) {
date365 = date365 + monthLength[i];
}
date365 = date365 + currentDay; // Done!
add a comment |
I think this is more straightforward:
var date365 = 0;
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var currentMonth = currentDate.getMonth();
var currentDay = currentDate.getDate();
var monthLength = [31,28,31,30,31,30,31,31,30,31,30,31];
var leapYear = new Date(currentYear, 1, 29);
if (leapYear.getDate() == 29) { // If it's a leap year, changes 28 to 29
monthLength[1] = 29;
}
for ( i=0; i < currentMonth; i++ ) {
date365 = date365 + monthLength[i];
}
date365 = date365 + currentDay; // Done!
I think this is more straightforward:
var date365 = 0;
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var currentMonth = currentDate.getMonth();
var currentDay = currentDate.getDate();
var monthLength = [31,28,31,30,31,30,31,31,30,31,30,31];
var leapYear = new Date(currentYear, 1, 29);
if (leapYear.getDate() == 29) { // If it's a leap year, changes 28 to 29
monthLength[1] = 29;
}
for ( i=0; i < currentMonth; i++ ) {
date365 = date365 + monthLength[i];
}
date365 = date365 + currentDay; // Done!
answered May 1 '18 at 11:53
SeanSean
425
425
add a comment |
add a comment |
I've made one that's readable and will do the trick very quickly, as well as handle JS Date objects with disparate time zones.
I've included quite a few test cases for time zones, DST, leap seconds and Leap years.
P.S. ECMA-262 ignores leap seconds, unlike UTC. If you were to convert this to a language that uses real UTC, you could just add 1 to oneDay
.
// returns 1 - 366
findDayOfYear = function (date) {
var oneDay = 1000 * 60 * 60 * 24; // A day in milliseconds
var og = { // Saving original data
ts: date.getTime(),
dom: date.getDate(), // We don't need to save hours/minutes because DST is never at 12am.
month: date.getMonth()
}
date.setDate(1); // Sets Date of the Month to the 1st.
date.setMonth(0); // Months are zero based in JS's Date object
var start_ts = date.getTime(); // New Year's Midnight JS Timestamp
var diff = og.ts - start_ts;
date.setDate(og.dom); // Revert back to original date object
date.setMonth(og.month); // This method does preserve timezone
return Math.round(diff / oneDay) + 1; // Deals with DST globally. Ceil fails in Australia. Floor Fails in US.
}
// Tests
var pre_start_dst = new Date(2016, 2, 12);
var on_start_dst = new Date(2016, 2, 13);
var post_start_dst = new Date(2016, 2, 14);
var pre_end_dst_date = new Date(2016, 10, 5);
var on_end_dst_date = new Date(2016, 10, 6);
var post_end_dst_date = new Date(2016, 10, 7);
var pre_leap_second = new Date(2015, 5, 29);
var on_leap_second = new Date(2015, 5, 30);
var post_leap_second = new Date(2015, 6, 1);
// 2012 was a leap year with a leap second in june 30th
var leap_second_december31_premidnight = new Date(2012, 11, 31, 23, 59, 59, 999);
var january1 = new Date(2016, 0, 1);
var january31 = new Date(2016, 0, 31);
var december31 = new Date(2015, 11, 31);
var leap_december31 = new Date(2016, 11, 31);
alert( ""
+ "nPre Start DST: " + findDayOfYear(pre_start_dst) + " === 72"
+ "nOn Start DST: " + findDayOfYear(on_start_dst) + " === 73"
+ "nPost Start DST: " + findDayOfYear(post_start_dst) + " === 74"
+ "nPre Leap Second: " + findDayOfYear(pre_leap_second) + " === 180"
+ "nOn Leap Second: " + findDayOfYear(on_leap_second) + " === 181"
+ "nPost Leap Second: " + findDayOfYear(post_leap_second) + " === 182"
+ "nPre End DST: " + findDayOfYear(pre_end_dst_date) + " === 310"
+ "nOn End DST: " + findDayOfYear(on_end_dst_date) + " === 311"
+ "nPost End DST: " + findDayOfYear(post_end_dst_date) + " === 312"
+ "nJanuary 1st: " + findDayOfYear(january1) + " === 1"
+ "nJanuary 31st: " + findDayOfYear(january31) + " === 31"
+ "nNormal December 31st: " + findDayOfYear(december31) + " === 365"
+ "nLeap December 31st: " + findDayOfYear(leap_december31) + " === 366"
+ "nLast Second of Double Leap: " + findDayOfYear(leap_second_december31_premidnight) + " === 366"
);
add a comment |
I've made one that's readable and will do the trick very quickly, as well as handle JS Date objects with disparate time zones.
I've included quite a few test cases for time zones, DST, leap seconds and Leap years.
P.S. ECMA-262 ignores leap seconds, unlike UTC. If you were to convert this to a language that uses real UTC, you could just add 1 to oneDay
.
// returns 1 - 366
findDayOfYear = function (date) {
var oneDay = 1000 * 60 * 60 * 24; // A day in milliseconds
var og = { // Saving original data
ts: date.getTime(),
dom: date.getDate(), // We don't need to save hours/minutes because DST is never at 12am.
month: date.getMonth()
}
date.setDate(1); // Sets Date of the Month to the 1st.
date.setMonth(0); // Months are zero based in JS's Date object
var start_ts = date.getTime(); // New Year's Midnight JS Timestamp
var diff = og.ts - start_ts;
date.setDate(og.dom); // Revert back to original date object
date.setMonth(og.month); // This method does preserve timezone
return Math.round(diff / oneDay) + 1; // Deals with DST globally. Ceil fails in Australia. Floor Fails in US.
}
// Tests
var pre_start_dst = new Date(2016, 2, 12);
var on_start_dst = new Date(2016, 2, 13);
var post_start_dst = new Date(2016, 2, 14);
var pre_end_dst_date = new Date(2016, 10, 5);
var on_end_dst_date = new Date(2016, 10, 6);
var post_end_dst_date = new Date(2016, 10, 7);
var pre_leap_second = new Date(2015, 5, 29);
var on_leap_second = new Date(2015, 5, 30);
var post_leap_second = new Date(2015, 6, 1);
// 2012 was a leap year with a leap second in june 30th
var leap_second_december31_premidnight = new Date(2012, 11, 31, 23, 59, 59, 999);
var january1 = new Date(2016, 0, 1);
var january31 = new Date(2016, 0, 31);
var december31 = new Date(2015, 11, 31);
var leap_december31 = new Date(2016, 11, 31);
alert( ""
+ "nPre Start DST: " + findDayOfYear(pre_start_dst) + " === 72"
+ "nOn Start DST: " + findDayOfYear(on_start_dst) + " === 73"
+ "nPost Start DST: " + findDayOfYear(post_start_dst) + " === 74"
+ "nPre Leap Second: " + findDayOfYear(pre_leap_second) + " === 180"
+ "nOn Leap Second: " + findDayOfYear(on_leap_second) + " === 181"
+ "nPost Leap Second: " + findDayOfYear(post_leap_second) + " === 182"
+ "nPre End DST: " + findDayOfYear(pre_end_dst_date) + " === 310"
+ "nOn End DST: " + findDayOfYear(on_end_dst_date) + " === 311"
+ "nPost End DST: " + findDayOfYear(post_end_dst_date) + " === 312"
+ "nJanuary 1st: " + findDayOfYear(january1) + " === 1"
+ "nJanuary 31st: " + findDayOfYear(january31) + " === 31"
+ "nNormal December 31st: " + findDayOfYear(december31) + " === 365"
+ "nLeap December 31st: " + findDayOfYear(leap_december31) + " === 366"
+ "nLast Second of Double Leap: " + findDayOfYear(leap_second_december31_premidnight) + " === 366"
);
add a comment |
I've made one that's readable and will do the trick very quickly, as well as handle JS Date objects with disparate time zones.
I've included quite a few test cases for time zones, DST, leap seconds and Leap years.
P.S. ECMA-262 ignores leap seconds, unlike UTC. If you were to convert this to a language that uses real UTC, you could just add 1 to oneDay
.
// returns 1 - 366
findDayOfYear = function (date) {
var oneDay = 1000 * 60 * 60 * 24; // A day in milliseconds
var og = { // Saving original data
ts: date.getTime(),
dom: date.getDate(), // We don't need to save hours/minutes because DST is never at 12am.
month: date.getMonth()
}
date.setDate(1); // Sets Date of the Month to the 1st.
date.setMonth(0); // Months are zero based in JS's Date object
var start_ts = date.getTime(); // New Year's Midnight JS Timestamp
var diff = og.ts - start_ts;
date.setDate(og.dom); // Revert back to original date object
date.setMonth(og.month); // This method does preserve timezone
return Math.round(diff / oneDay) + 1; // Deals with DST globally. Ceil fails in Australia. Floor Fails in US.
}
// Tests
var pre_start_dst = new Date(2016, 2, 12);
var on_start_dst = new Date(2016, 2, 13);
var post_start_dst = new Date(2016, 2, 14);
var pre_end_dst_date = new Date(2016, 10, 5);
var on_end_dst_date = new Date(2016, 10, 6);
var post_end_dst_date = new Date(2016, 10, 7);
var pre_leap_second = new Date(2015, 5, 29);
var on_leap_second = new Date(2015, 5, 30);
var post_leap_second = new Date(2015, 6, 1);
// 2012 was a leap year with a leap second in june 30th
var leap_second_december31_premidnight = new Date(2012, 11, 31, 23, 59, 59, 999);
var january1 = new Date(2016, 0, 1);
var january31 = new Date(2016, 0, 31);
var december31 = new Date(2015, 11, 31);
var leap_december31 = new Date(2016, 11, 31);
alert( ""
+ "nPre Start DST: " + findDayOfYear(pre_start_dst) + " === 72"
+ "nOn Start DST: " + findDayOfYear(on_start_dst) + " === 73"
+ "nPost Start DST: " + findDayOfYear(post_start_dst) + " === 74"
+ "nPre Leap Second: " + findDayOfYear(pre_leap_second) + " === 180"
+ "nOn Leap Second: " + findDayOfYear(on_leap_second) + " === 181"
+ "nPost Leap Second: " + findDayOfYear(post_leap_second) + " === 182"
+ "nPre End DST: " + findDayOfYear(pre_end_dst_date) + " === 310"
+ "nOn End DST: " + findDayOfYear(on_end_dst_date) + " === 311"
+ "nPost End DST: " + findDayOfYear(post_end_dst_date) + " === 312"
+ "nJanuary 1st: " + findDayOfYear(january1) + " === 1"
+ "nJanuary 31st: " + findDayOfYear(january31) + " === 31"
+ "nNormal December 31st: " + findDayOfYear(december31) + " === 365"
+ "nLeap December 31st: " + findDayOfYear(leap_december31) + " === 366"
+ "nLast Second of Double Leap: " + findDayOfYear(leap_second_december31_premidnight) + " === 366"
);
I've made one that's readable and will do the trick very quickly, as well as handle JS Date objects with disparate time zones.
I've included quite a few test cases for time zones, DST, leap seconds and Leap years.
P.S. ECMA-262 ignores leap seconds, unlike UTC. If you were to convert this to a language that uses real UTC, you could just add 1 to oneDay
.
// returns 1 - 366
findDayOfYear = function (date) {
var oneDay = 1000 * 60 * 60 * 24; // A day in milliseconds
var og = { // Saving original data
ts: date.getTime(),
dom: date.getDate(), // We don't need to save hours/minutes because DST is never at 12am.
month: date.getMonth()
}
date.setDate(1); // Sets Date of the Month to the 1st.
date.setMonth(0); // Months are zero based in JS's Date object
var start_ts = date.getTime(); // New Year's Midnight JS Timestamp
var diff = og.ts - start_ts;
date.setDate(og.dom); // Revert back to original date object
date.setMonth(og.month); // This method does preserve timezone
return Math.round(diff / oneDay) + 1; // Deals with DST globally. Ceil fails in Australia. Floor Fails in US.
}
// Tests
var pre_start_dst = new Date(2016, 2, 12);
var on_start_dst = new Date(2016, 2, 13);
var post_start_dst = new Date(2016, 2, 14);
var pre_end_dst_date = new Date(2016, 10, 5);
var on_end_dst_date = new Date(2016, 10, 6);
var post_end_dst_date = new Date(2016, 10, 7);
var pre_leap_second = new Date(2015, 5, 29);
var on_leap_second = new Date(2015, 5, 30);
var post_leap_second = new Date(2015, 6, 1);
// 2012 was a leap year with a leap second in june 30th
var leap_second_december31_premidnight = new Date(2012, 11, 31, 23, 59, 59, 999);
var january1 = new Date(2016, 0, 1);
var january31 = new Date(2016, 0, 31);
var december31 = new Date(2015, 11, 31);
var leap_december31 = new Date(2016, 11, 31);
alert( ""
+ "nPre Start DST: " + findDayOfYear(pre_start_dst) + " === 72"
+ "nOn Start DST: " + findDayOfYear(on_start_dst) + " === 73"
+ "nPost Start DST: " + findDayOfYear(post_start_dst) + " === 74"
+ "nPre Leap Second: " + findDayOfYear(pre_leap_second) + " === 180"
+ "nOn Leap Second: " + findDayOfYear(on_leap_second) + " === 181"
+ "nPost Leap Second: " + findDayOfYear(post_leap_second) + " === 182"
+ "nPre End DST: " + findDayOfYear(pre_end_dst_date) + " === 310"
+ "nOn End DST: " + findDayOfYear(on_end_dst_date) + " === 311"
+ "nPost End DST: " + findDayOfYear(post_end_dst_date) + " === 312"
+ "nJanuary 1st: " + findDayOfYear(january1) + " === 1"
+ "nJanuary 31st: " + findDayOfYear(january31) + " === 31"
+ "nNormal December 31st: " + findDayOfYear(december31) + " === 365"
+ "nLeap December 31st: " + findDayOfYear(leap_december31) + " === 366"
+ "nLast Second of Double Leap: " + findDayOfYear(leap_second_december31_premidnight) + " === 366"
);
// returns 1 - 366
findDayOfYear = function (date) {
var oneDay = 1000 * 60 * 60 * 24; // A day in milliseconds
var og = { // Saving original data
ts: date.getTime(),
dom: date.getDate(), // We don't need to save hours/minutes because DST is never at 12am.
month: date.getMonth()
}
date.setDate(1); // Sets Date of the Month to the 1st.
date.setMonth(0); // Months are zero based in JS's Date object
var start_ts = date.getTime(); // New Year's Midnight JS Timestamp
var diff = og.ts - start_ts;
date.setDate(og.dom); // Revert back to original date object
date.setMonth(og.month); // This method does preserve timezone
return Math.round(diff / oneDay) + 1; // Deals with DST globally. Ceil fails in Australia. Floor Fails in US.
}
// Tests
var pre_start_dst = new Date(2016, 2, 12);
var on_start_dst = new Date(2016, 2, 13);
var post_start_dst = new Date(2016, 2, 14);
var pre_end_dst_date = new Date(2016, 10, 5);
var on_end_dst_date = new Date(2016, 10, 6);
var post_end_dst_date = new Date(2016, 10, 7);
var pre_leap_second = new Date(2015, 5, 29);
var on_leap_second = new Date(2015, 5, 30);
var post_leap_second = new Date(2015, 6, 1);
// 2012 was a leap year with a leap second in june 30th
var leap_second_december31_premidnight = new Date(2012, 11, 31, 23, 59, 59, 999);
var january1 = new Date(2016, 0, 1);
var january31 = new Date(2016, 0, 31);
var december31 = new Date(2015, 11, 31);
var leap_december31 = new Date(2016, 11, 31);
alert( ""
+ "nPre Start DST: " + findDayOfYear(pre_start_dst) + " === 72"
+ "nOn Start DST: " + findDayOfYear(on_start_dst) + " === 73"
+ "nPost Start DST: " + findDayOfYear(post_start_dst) + " === 74"
+ "nPre Leap Second: " + findDayOfYear(pre_leap_second) + " === 180"
+ "nOn Leap Second: " + findDayOfYear(on_leap_second) + " === 181"
+ "nPost Leap Second: " + findDayOfYear(post_leap_second) + " === 182"
+ "nPre End DST: " + findDayOfYear(pre_end_dst_date) + " === 310"
+ "nOn End DST: " + findDayOfYear(on_end_dst_date) + " === 311"
+ "nPost End DST: " + findDayOfYear(post_end_dst_date) + " === 312"
+ "nJanuary 1st: " + findDayOfYear(january1) + " === 1"
+ "nJanuary 31st: " + findDayOfYear(january31) + " === 31"
+ "nNormal December 31st: " + findDayOfYear(december31) + " === 365"
+ "nLeap December 31st: " + findDayOfYear(leap_december31) + " === 366"
+ "nLast Second of Double Leap: " + findDayOfYear(leap_second_december31_premidnight) + " === 366"
);
// returns 1 - 366
findDayOfYear = function (date) {
var oneDay = 1000 * 60 * 60 * 24; // A day in milliseconds
var og = { // Saving original data
ts: date.getTime(),
dom: date.getDate(), // We don't need to save hours/minutes because DST is never at 12am.
month: date.getMonth()
}
date.setDate(1); // Sets Date of the Month to the 1st.
date.setMonth(0); // Months are zero based in JS's Date object
var start_ts = date.getTime(); // New Year's Midnight JS Timestamp
var diff = og.ts - start_ts;
date.setDate(og.dom); // Revert back to original date object
date.setMonth(og.month); // This method does preserve timezone
return Math.round(diff / oneDay) + 1; // Deals with DST globally. Ceil fails in Australia. Floor Fails in US.
}
// Tests
var pre_start_dst = new Date(2016, 2, 12);
var on_start_dst = new Date(2016, 2, 13);
var post_start_dst = new Date(2016, 2, 14);
var pre_end_dst_date = new Date(2016, 10, 5);
var on_end_dst_date = new Date(2016, 10, 6);
var post_end_dst_date = new Date(2016, 10, 7);
var pre_leap_second = new Date(2015, 5, 29);
var on_leap_second = new Date(2015, 5, 30);
var post_leap_second = new Date(2015, 6, 1);
// 2012 was a leap year with a leap second in june 30th
var leap_second_december31_premidnight = new Date(2012, 11, 31, 23, 59, 59, 999);
var january1 = new Date(2016, 0, 1);
var january31 = new Date(2016, 0, 31);
var december31 = new Date(2015, 11, 31);
var leap_december31 = new Date(2016, 11, 31);
alert( ""
+ "nPre Start DST: " + findDayOfYear(pre_start_dst) + " === 72"
+ "nOn Start DST: " + findDayOfYear(on_start_dst) + " === 73"
+ "nPost Start DST: " + findDayOfYear(post_start_dst) + " === 74"
+ "nPre Leap Second: " + findDayOfYear(pre_leap_second) + " === 180"
+ "nOn Leap Second: " + findDayOfYear(on_leap_second) + " === 181"
+ "nPost Leap Second: " + findDayOfYear(post_leap_second) + " === 182"
+ "nPre End DST: " + findDayOfYear(pre_end_dst_date) + " === 310"
+ "nOn End DST: " + findDayOfYear(on_end_dst_date) + " === 311"
+ "nPost End DST: " + findDayOfYear(post_end_dst_date) + " === 312"
+ "nJanuary 1st: " + findDayOfYear(january1) + " === 1"
+ "nJanuary 31st: " + findDayOfYear(january31) + " === 31"
+ "nNormal December 31st: " + findDayOfYear(december31) + " === 365"
+ "nLeap December 31st: " + findDayOfYear(leap_december31) + " === 366"
+ "nLast Second of Double Leap: " + findDayOfYear(leap_second_december31_premidnight) + " === 366"
);
edited Mar 11 '16 at 18:02
answered Mar 10 '16 at 17:52
Ray FossRay Foss
2,24611419
2,24611419
add a comment |
add a comment |
I would like to provide a solution that does calculations adding the days for each previous month:
function getDayOfYear(date) {
var month = date.getMonth();
var year = date.getFullYear();
var days = date.getDate();
for (var i = 0; i < month; i++) {
days += new Date(year, i+1, 0).getDate();
}
return days;
}
var input = new Date(2017, 7, 5);
console.log(input);
console.log(getDayOfYear(input));
This way you don't have to manage the details of leap years and daylight saving.
add a comment |
I would like to provide a solution that does calculations adding the days for each previous month:
function getDayOfYear(date) {
var month = date.getMonth();
var year = date.getFullYear();
var days = date.getDate();
for (var i = 0; i < month; i++) {
days += new Date(year, i+1, 0).getDate();
}
return days;
}
var input = new Date(2017, 7, 5);
console.log(input);
console.log(getDayOfYear(input));
This way you don't have to manage the details of leap years and daylight saving.
add a comment |
I would like to provide a solution that does calculations adding the days for each previous month:
function getDayOfYear(date) {
var month = date.getMonth();
var year = date.getFullYear();
var days = date.getDate();
for (var i = 0; i < month; i++) {
days += new Date(year, i+1, 0).getDate();
}
return days;
}
var input = new Date(2017, 7, 5);
console.log(input);
console.log(getDayOfYear(input));
This way you don't have to manage the details of leap years and daylight saving.
I would like to provide a solution that does calculations adding the days for each previous month:
function getDayOfYear(date) {
var month = date.getMonth();
var year = date.getFullYear();
var days = date.getDate();
for (var i = 0; i < month; i++) {
days += new Date(year, i+1, 0).getDate();
}
return days;
}
var input = new Date(2017, 7, 5);
console.log(input);
console.log(getDayOfYear(input));
This way you don't have to manage the details of leap years and daylight saving.
function getDayOfYear(date) {
var month = date.getMonth();
var year = date.getFullYear();
var days = date.getDate();
for (var i = 0; i < month; i++) {
days += new Date(year, i+1, 0).getDate();
}
return days;
}
var input = new Date(2017, 7, 5);
console.log(input);
console.log(getDayOfYear(input));
function getDayOfYear(date) {
var month = date.getMonth();
var year = date.getFullYear();
var days = date.getDate();
for (var i = 0; i < month; i++) {
days += new Date(year, i+1, 0).getDate();
}
return days;
}
var input = new Date(2017, 7, 5);
console.log(input);
console.log(getDayOfYear(input));
answered May 10 '17 at 12:58
manixmanix
12
12
add a comment |
add a comment |
A alternative using UTC timestamps. Also as others noted the day indicating 1st a month is 1 rather than 0. The month starts at 0 however.
var now = Date.now();
var year = new Date().getUTCFullYear();
var year_start = Date.UTC(year, 0, 1);
var day_length_in_ms = 1000*60*60*24;
var day_number = Math.floor((now - year_start)/day_length_in_ms)
console.log("Day of year " + day_number);
add a comment |
A alternative using UTC timestamps. Also as others noted the day indicating 1st a month is 1 rather than 0. The month starts at 0 however.
var now = Date.now();
var year = new Date().getUTCFullYear();
var year_start = Date.UTC(year, 0, 1);
var day_length_in_ms = 1000*60*60*24;
var day_number = Math.floor((now - year_start)/day_length_in_ms)
console.log("Day of year " + day_number);
add a comment |
A alternative using UTC timestamps. Also as others noted the day indicating 1st a month is 1 rather than 0. The month starts at 0 however.
var now = Date.now();
var year = new Date().getUTCFullYear();
var year_start = Date.UTC(year, 0, 1);
var day_length_in_ms = 1000*60*60*24;
var day_number = Math.floor((now - year_start)/day_length_in_ms)
console.log("Day of year " + day_number);
A alternative using UTC timestamps. Also as others noted the day indicating 1st a month is 1 rather than 0. The month starts at 0 however.
var now = Date.now();
var year = new Date().getUTCFullYear();
var year_start = Date.UTC(year, 0, 1);
var day_length_in_ms = 1000*60*60*24;
var day_number = Math.floor((now - year_start)/day_length_in_ms)
console.log("Day of year " + day_number);
answered Jul 24 '17 at 13:32
emctwooemctwoo
1
1
add a comment |
add a comment |
You can pass parameter as date number in setDate
function:
var targetDate = new Date();
targetDate.setDate(1);
// Now we can see the expected date as: Mon Jan 01 2018 01:43:24
console.log(targetDate);
targetDate.setDate(365);
// You can see: Mon Dec 31 2018 01:44:47
console.log(targetDate)
add a comment |
You can pass parameter as date number in setDate
function:
var targetDate = new Date();
targetDate.setDate(1);
// Now we can see the expected date as: Mon Jan 01 2018 01:43:24
console.log(targetDate);
targetDate.setDate(365);
// You can see: Mon Dec 31 2018 01:44:47
console.log(targetDate)
add a comment |
You can pass parameter as date number in setDate
function:
var targetDate = new Date();
targetDate.setDate(1);
// Now we can see the expected date as: Mon Jan 01 2018 01:43:24
console.log(targetDate);
targetDate.setDate(365);
// You can see: Mon Dec 31 2018 01:44:47
console.log(targetDate)
You can pass parameter as date number in setDate
function:
var targetDate = new Date();
targetDate.setDate(1);
// Now we can see the expected date as: Mon Jan 01 2018 01:43:24
console.log(targetDate);
targetDate.setDate(365);
// You can see: Mon Dec 31 2018 01:44:47
console.log(targetDate)
answered Jan 27 '18 at 20:45
Ilyas karimIlyas karim
1,37211727
1,37211727
add a comment |
add a comment |
This might be useful to those who need the day of the year as a string and have jQuery UI available.
You can use jQuery UI Datepicker:
day_of_year_string = $.datepicker.formatDate("o", new Date())
Underneath it works the same way as some of the answers already mentioned ((date_ms - first_date_of_year_ms) / ms_per_day
):
function getDayOfTheYearFromDate(d) {
return Math.round((new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime()
- new Date(d.getFullYear(), 0, 0).getTime()) / 86400000);
}
day_of_year_int = getDayOfTheYearFromDate(new Date())
add a comment |
This might be useful to those who need the day of the year as a string and have jQuery UI available.
You can use jQuery UI Datepicker:
day_of_year_string = $.datepicker.formatDate("o", new Date())
Underneath it works the same way as some of the answers already mentioned ((date_ms - first_date_of_year_ms) / ms_per_day
):
function getDayOfTheYearFromDate(d) {
return Math.round((new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime()
- new Date(d.getFullYear(), 0, 0).getTime()) / 86400000);
}
day_of_year_int = getDayOfTheYearFromDate(new Date())
add a comment |
This might be useful to those who need the day of the year as a string and have jQuery UI available.
You can use jQuery UI Datepicker:
day_of_year_string = $.datepicker.formatDate("o", new Date())
Underneath it works the same way as some of the answers already mentioned ((date_ms - first_date_of_year_ms) / ms_per_day
):
function getDayOfTheYearFromDate(d) {
return Math.round((new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime()
- new Date(d.getFullYear(), 0, 0).getTime()) / 86400000);
}
day_of_year_int = getDayOfTheYearFromDate(new Date())
This might be useful to those who need the day of the year as a string and have jQuery UI available.
You can use jQuery UI Datepicker:
day_of_year_string = $.datepicker.formatDate("o", new Date())
Underneath it works the same way as some of the answers already mentioned ((date_ms - first_date_of_year_ms) / ms_per_day
):
function getDayOfTheYearFromDate(d) {
return Math.round((new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime()
- new Date(d.getFullYear(), 0, 0).getTime()) / 86400000);
}
day_of_year_int = getDayOfTheYearFromDate(new Date())
edited Nov 15 '18 at 20:38
answered Nov 15 '18 at 20:28
Adrian G.Adrian G.
113
113
add a comment |
add a comment |
It always get's me worried when mixing maths with date functions (it's so easy to miss some leap year other detail). Say you have:
var d = new Date();
I would suggest using the following, days will be saved in day
:
for(var day = d.getDate(); d.getMonth(); day += d.getDate())
d.setDate(0);
Can't see any reason why this wouldn't work just fine (and I wouldn't be so worried about the few iterations since this will not be used so intensively).
add a comment |
It always get's me worried when mixing maths with date functions (it's so easy to miss some leap year other detail). Say you have:
var d = new Date();
I would suggest using the following, days will be saved in day
:
for(var day = d.getDate(); d.getMonth(); day += d.getDate())
d.setDate(0);
Can't see any reason why this wouldn't work just fine (and I wouldn't be so worried about the few iterations since this will not be used so intensively).
add a comment |
It always get's me worried when mixing maths with date functions (it's so easy to miss some leap year other detail). Say you have:
var d = new Date();
I would suggest using the following, days will be saved in day
:
for(var day = d.getDate(); d.getMonth(); day += d.getDate())
d.setDate(0);
Can't see any reason why this wouldn't work just fine (and I wouldn't be so worried about the few iterations since this will not be used so intensively).
It always get's me worried when mixing maths with date functions (it's so easy to miss some leap year other detail). Say you have:
var d = new Date();
I would suggest using the following, days will be saved in day
:
for(var day = d.getDate(); d.getMonth(); day += d.getDate())
d.setDate(0);
Can't see any reason why this wouldn't work just fine (and I wouldn't be so worried about the few iterations since this will not be used so intensively).
answered Feb 12 '15 at 16:03
Martin EkblomMartin Ekblom
111
111
add a comment |
add a comment |
/*USE THIS SCRIPT */
var today = new Date();
var first = new Date(today.getFullYear(), 0, 1);
var theDay = Math.round(((today - first) / 1000 / 60 / 60 / 24) + .5, 0);
alert("Today is the " + theDay + (theDay == 1 ? "st" : (theDay == 2 ? "nd" : (theDay == 3 ? "rd" : "th"))) + " day of the year");
add a comment |
/*USE THIS SCRIPT */
var today = new Date();
var first = new Date(today.getFullYear(), 0, 1);
var theDay = Math.round(((today - first) / 1000 / 60 / 60 / 24) + .5, 0);
alert("Today is the " + theDay + (theDay == 1 ? "st" : (theDay == 2 ? "nd" : (theDay == 3 ? "rd" : "th"))) + " day of the year");
add a comment |
/*USE THIS SCRIPT */
var today = new Date();
var first = new Date(today.getFullYear(), 0, 1);
var theDay = Math.round(((today - first) / 1000 / 60 / 60 / 24) + .5, 0);
alert("Today is the " + theDay + (theDay == 1 ? "st" : (theDay == 2 ? "nd" : (theDay == 3 ? "rd" : "th"))) + " day of the year");
/*USE THIS SCRIPT */
var today = new Date();
var first = new Date(today.getFullYear(), 0, 1);
var theDay = Math.round(((today - first) / 1000 / 60 / 60 / 24) + .5, 0);
alert("Today is the " + theDay + (theDay == 1 ? "st" : (theDay == 2 ? "nd" : (theDay == 3 ? "rd" : "th"))) + " day of the year");
edited Aug 25 '17 at 12:42
il_raffa
4,589122230
4,589122230
answered Aug 25 '17 at 12:19
Pankaj YadavPankaj Yadav
394
394
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8619879%2fjavascript-calculate-the-day-of-the-year-1-366%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
var days = new Date().getFullYear() % 4 == 0 ? 366 : 365;
– Alex Turpin
Dec 23 '11 at 19:27
But really I'm not sure what you mean. You just want the number of days in the year? Or between two dates?
– Alex Turpin
Dec 23 '11 at 19:28
12
fyi @xeon06, leap year calculation is a bit more complicated than mod'ing by 4. see: leap year algorithm
– Matt Felzani
Dec 23 '11 at 19:29
4
@Xeon06: That's correct only most of the time. From Wikipedia: Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years.
– Cameron
Dec 23 '11 at 19:30
Ah well I stand corrected. @minitech seems to have the right answer then.
– Alex Turpin
Dec 23 '11 at 19:31