In computer terms, new Date[]
and regular expression
solutions are slow! If you want a super-fast [and super-cryptic] one-liner, try this one [assuming m
is in Jan=1
format]. I keep trying different code changes to get the best performance.
My current fastest version:
After looking at this related question Leap year check using bitwise operators [amazing speed] and discovering what the 25 & 15 magic number represented, I have come up with this optimized hybrid of answers:
function getDaysInMonth[m, y] {
return m===2 ? y & 3 || ![y%25] && y & 15 ? 28 : 29 : 30 + [m+[m>>3]&1];
}
Given the bit-shifting this obviously assumes that your m
& y
parameters are both integers, as passing numbers as strings would result
in weird results.
JSFiddle: //jsfiddle.net/TrueBlueAussie/H89X3/22/
JSPerf results: //jsperf.com/days-in-month-head-to-head/5
For some reason, [m+[m>>3]&1]
is more efficient than [5546>>m&1]
on almost all browsers.
The only real competition for speed is from @GitaarLab, so I have created a head-to-head JSPerf for us to test on: //jsperf.com/days-in-month-head-to-head/5
It works based on my leap year answer here: javascript to find leap year this answer here Leap year check using bitwise operators [amazing speed] as well as the following binary logic.
A quick lesson in binary months:
If you interpret the index of the desired months [Jan = 1] in binary you will notice that months with 31 days either have bit 3 clear and bit 0 set, or bit 3 set and bit 0 clear.
Jan = 1 = 0001 : 31 days
Feb = 2 = 0010
Mar = 3 = 0011 : 31 days
Apr = 4 = 0100
May = 5 = 0101 : 31 days
Jun = 6 = 0110
Jul = 7 = 0111 : 31 days
Aug = 8 = 1000 : 31 days
Sep = 9 = 1001
Oct = 10 = 1010 : 31 days
Nov = 11 = 1011
Dec = 12 = 1100 : 31 days
That means you
can shift the value 3 places with >> 3
, XOR the bits with the original ^ m
and see if the result is 1
or 0
in bit position 0 using & 1
. Note: It turns out +
is slightly faster than XOR [^
] and [m >> 3] + m
gives the same result in bit 0.
JSPerf results: //jsperf.com/days-in-month-perf-test/6
JavaScript Datetime: Exercise-9 with Solution
Write a JavaScript function to get the last day of a month.
Test Data:
console.log[lastday[2014,0]];
console.log[lastday[2014,1]];
console.log[lastday[2014,11]];
Output :
31
28
31
Sample Solution:-
HTML Code:
Find out the last day of a month
JavaScript Code:
var lastday = function[y,m]{
return new Date[y, m +1, 0].getDate[];
}
console.log[lastday[2014,0]];
console.log[lastday[2014,1]];
console.log[lastday[2014,11]];
Sample Output:
31 28 31
Flowchart:
Live Demo:
See the Pen JavaScript - Find out the last day of a month-date-ex- 9 by w3resource [@w3resource] on CodePen.
Improve this sample solution and post your code through Disqus
Previous: Write a JavaScript function to get difference between two dates in days.
Next: Write a JavaScript function to calculate 'yesterday day'.
JavaScript: Tips of the Day
Reduce method
[1, 2, 3, 4].reduce[[x, y] => console.log[x, y]];
The first argument that the reduce method receives is the accumulator, x in this case. The second argument is the current value, y. With the reduce method, we execute a callback function on every element in the array, which could ultimately result in one single value.
In this example, we are not returning any values, we are simply logging the values of the accumulator and the current value.
The value of the accumulator is equal to the previously returned
value of the callback function. If you don't pass the optional initialValue argument to the reduce method, the accumulator is equal to the first element on the first call.
On the first call, the accumulator [x] is 1, and the current value [y] is 2. We don't return from the callback function, we log the accumulator and current value: 1 and 2 get logged.
If you don't return a value from a function, it returns undefined. On the next call, the accumulator is undefined, and the current value
is 3. undefined and 3 get logged.
On the fourth call, we again don't return from the callback function. The accumulator is again undefined, and the current value is 4. undefined and 4 get logged.
Ref: //bit.ly/323Y0P6