http://gengns.com/wp-content/uploads/2015/05/logo-onepage-300x66.png00Génesishttp://gengns.com/wp-content/uploads/2015/05/logo-onepage-300x66.pngGénesis2017-08-29 13:33:282017-08-29 13:33:28Get all days in a month that start in a certain day of the week
//If you want to do it right it’s harder than it looks
function get_days_from_weekday_and_month_GOOD(weekday, month, year) {
if (!year) year = (new Date()).getFullYear();
month = (month-1)%12;
//japanfever magic here E3839EE383AAE382AAE9B3A9E38388E383ACE383AD
var monthdays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
var monthstep = [ 7, 3, 3, 6, 8, 4, 6, 2, 5, 7, 3, 5 ];
var days = [];
//Leap years should be taken into account
leap = 0;
if (((year)%4 == 0 && (year)%100 != 0) || (year)%400 == 0) {
monthdays[1] = 29;
if (month < 2) leap = 1;
}
var inc = 8 – ((monthstep[month] + (year%100) + parseInt((year%100)/4) –
2*parseInt((year/100)%4) – leap) % 7);
var d = (weekday + inc)%7;
while (d get_days_from_weekday_and_month_GOOD(6, 7)
[ 1, 8, 15, 22, 29 ]
I really appreciate your extremely useful comments, I would like to meet you if you visit anytime South of Spain xD
japanfever says:
Your blog cut my post. I try again
//If you want to do it right it’s harder than it looks E3839EE383AAE382AAE9B3A9E38388E383ACE383AD
function get_days_from_weekday_and_month_GOOD(weekday, month, year) {
if (!year) year = (new Date()).getFullYear();
month = (month-1)%12;
//japanfever magic here E3839EE383AAE382AAE9B3A9E38388E383ACE383AD
var monthdays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
var monthstep = [ 7, 3, 3, 6, 8, 4, 6, 2, 5, 7, 3, 5 ];
var days = [];
//Leap years should be taken into account
leap = 0;
if (((year)%4 == 0 && (year)%100 != 0) || (year)%400 == 0) {
monthdays[1] = 29;
if (month < 2) leap = 1;
}
var inc = 8 – ((monthstep[month] + (year%100) + parseInt((year%100)/4) –
2*parseInt((year/100)%4) – leap) % 7);
var d = (weekday + inc)%7;
while (d <= monthdays[month]) {
days.push(d);
d += 7
}
return days;
}
Génesis says:
Thanks japanfever, you are right, there is an infinte loop and sometimes some days are missing.
I fixed with:
if (weekday < 0 || weekday > 6) return ‘Weekday must be between 0 and 6’
if (month < 1 || month > 12) return ‘Month must be between 1 and 12’
And
d.setDate(0)
This last line is for the missing days.
Your implementation is quite appeling, I like you oldschool way and your magic :P, your code is valid in any programming language but as you said is a little bit harder so I end up fixing mine!
japanfever says:
//Sorry, i dont speak english, only code, in japanese is my sign: E3839EE383AAE382AAE9B3A9E38388E383ACE383AD
//i’m 14 old and I’m forbidden to talk to strangers, i visit hundreds of blogs looking for personal challenges to better program
//Ok, now without magic, using the Javascript Date Object as you
//Slower than the other, but faster than yours, cause i now use only one loop
function get_days_from_weekday_and_month_BEST(weekday, month, year) {
if (!year) year = (new Date()).getFullYear();
var days = [];
var inc = 8 – (new Date(year, month-1, 1)).getDay();
var d = (weekday + inc)%7;
var monthdays = (new Date(year, month, 0)).getDate();
while (d <= monthdays) {
days.push(d);
d += 7
}
return days;
}
Génesis says:
OMG! 14 yo already, Elon Musk taught himself computer programming at the age of 12… 😛
Its WRONG in somre corner cases:
> get_days_from_weekday_and_month(6, 7)
[ 8, 15, 22, 29 ]
> //BAD !!!, in 2017 must be: [ 1, 8, 15, 22, 29 ]
> get_days_from_weekday_and_month(7, 7)
//Infinite loop
//If you want to do it right it’s harder than it looks
function get_days_from_weekday_and_month_GOOD(weekday, month, year) {
if (!year) year = (new Date()).getFullYear();
month = (month-1)%12;
//japanfever magic here E3839EE383AAE382AAE9B3A9E38388E383ACE383AD
var monthdays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
var monthstep = [ 7, 3, 3, 6, 8, 4, 6, 2, 5, 7, 3, 5 ];
var days = [];
//Leap years should be taken into account
leap = 0;
if (((year)%4 == 0 && (year)%100 != 0) || (year)%400 == 0) {
monthdays[1] = 29;
if (month < 2) leap = 1;
}
var inc = 8 – ((monthstep[month] + (year%100) + parseInt((year%100)/4) –
2*parseInt((year/100)%4) – leap) % 7);
var d = (weekday + inc)%7;
while (d get_days_from_weekday_and_month_GOOD(6, 7)
[ 1, 8, 15, 22, 29 ]
> get_days_from_weekday_and_month_GOOD(7, 7)
[ 2, 9, 16, 23, 30 ]
¯\_(ツ)_/¯
I really appreciate your extremely useful comments, I would like to meet you if you visit anytime South of Spain xD
Your blog cut my post. I try again
//If you want to do it right it’s harder than it looks E3839EE383AAE382AAE9B3A9E38388E383ACE383AD
function get_days_from_weekday_and_month_GOOD(weekday, month, year) {
if (!year) year = (new Date()).getFullYear();
month = (month-1)%12;
//japanfever magic here E3839EE383AAE382AAE9B3A9E38388E383ACE383AD
var monthdays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
var monthstep = [ 7, 3, 3, 6, 8, 4, 6, 2, 5, 7, 3, 5 ];
var days = [];
//Leap years should be taken into account
leap = 0;
if (((year)%4 == 0 && (year)%100 != 0) || (year)%400 == 0) {
monthdays[1] = 29;
if (month < 2) leap = 1;
}
var inc = 8 – ((monthstep[month] + (year%100) + parseInt((year%100)/4) –
2*parseInt((year/100)%4) – leap) % 7);
var d = (weekday + inc)%7;
while (d <= monthdays[month]) {
days.push(d);
d += 7
}
return days;
}
Thanks japanfever, you are right, there is an infinte loop and sometimes some days are missing.
I fixed with:
if (weekday < 0 || weekday > 6) return ‘Weekday must be between 0 and 6’
if (month < 1 || month > 12) return ‘Month must be between 1 and 12’
And
d.setDate(0)
This last line is for the missing days.
Your implementation is quite appeling, I like you oldschool way and your magic :P, your code is valid in any programming language but as you said is a little bit harder so I end up fixing mine!
//Sorry, i dont speak english, only code, in japanese is my sign: E3839EE383AAE382AAE9B3A9E38388E383ACE383AD
//i’m 14 old and I’m forbidden to talk to strangers, i visit hundreds of blogs looking for personal challenges to better program
//Ok, now without magic, using the Javascript Date Object as you
//Slower than the other, but faster than yours, cause i now use only one loop
function get_days_from_weekday_and_month_BEST(weekday, month, year) {
if (!year) year = (new Date()).getFullYear();
var days = [];
var inc = 8 – (new Date(year, month-1, 1)).getDay();
var d = (weekday + inc)%7;
var monthdays = (new Date(year, month, 0)).getDate();
while (d <= monthdays) {
days.push(d);
d += 7
}
return days;
}
OMG! 14 yo already, Elon Musk taught himself computer programming at the age of 12… 😛