How do I find the number of given days between two dates in Javascript? -
this question has answer here:
i have 2 arbitrary dates, lets april 1st 2012 , january 15th 2013. want calculate number of sundays, mondays, tuesdays, wednesdays, thursdays, fridays, , saturdays between 2 dates.
is there surefire-quick way without crippling users cpu/browser?
thanks
update
the premise of is, have defined average number of events given day of week. need calculate number of events happen in time period, partials (like 1/2 day of sunday half number of events added total)
ok, here possible untested solution
date1 = new date("2012-02-10"); date2 = new date("2012-03-10"); daysinbetween = (date2.gettime() - date1.gettime())/1000/3600/24; dayoftheweek1 = date1.getday(); weeks = parseint(daysinbetween/7, 10); extradays = daysinbetween%7;
you have weeks + 1
days of dayofweek1 ... dayofweek1 + (6 - extradays)
you have weeks + 1 + extradays
days of dayofweek1 + (6 - extradays) ... dayofweek1 + 6
please take acount if dayofweek1 === 6
assuming dayofweek1 + 1 === 0
.
edit:
a little bit more of code:
var days = {}; var dayoftheweekend = dayoftheweek1 + 6 - extradays; // no imagination names... if (dayoftheweekend < 6) { if (0 >= dayoftheweek1 && 0 <= dayoftheweekend) { days.sunday = weeks + 1; } else { days.sunday = weeks + 1 + extradays; } // etc other days, loog instead of 0 better. } else { // have go school! i'll edit later. // idea have take dayoftheweekend 0-6 range // , check if after dayofweek1 or before dayoftheweekend, days.sunday=weeks+1. }
Comments
Post a Comment