jquery - Javascript, advancing date range -
i using date-picker set start , end date. date range supposed move forward each day 2 week, sliding window of entry.
my code below works refactor easier maintain. techniques or tools can use improve code?
var challenge_start = new date(2015,04,04); var challenge_end = new date(2015,06,12); var range_start; var range_end; var today = new date(); var dd = today.getdate(); var mm = today.getmonth()+1; //january 0! var yyyy = today.getfullyear(); if(today<challenge_start) { range_start=challenge_start; range_end=challenge_start; } else if(today>challenge_start && today<challenge_end) { if(mm==5) { if(dd<18) { range_start=challenge_start; range_end=today; } else { range_start=today.setdate(today.getdate()-14); range_end=today; } } else if(mm=6) { if(dd<29) { range_start=today.setdate(today.getdate()-14); range_end=today; } else { range_start=today.setdate(today.getdate()-14); range_end=challenge_end; } } else if(mm==7) { if(dd<12) { range_start=today.setdate(today.getdate()-14); range_end=challenge_end; } else { range_start=challenge_end; range_end=challenge_end; } } } else if(today>challenge_end) { range_start=challenge_end; range_end=challenge_end; } $("#date").datepicker({ startdate: range_start, enddate: range_end, keyboardnavigation: false, autoclose: true, todayhighlight: true, language: langdate, disabletouchkeyboard: true });
i may not understand striving for, i'm pretty sure don't need have if(mm==x)
, if(dd=x)
statements.
perhaps want:
var challenge_start = new date(2015,04,04); var challenge_end = new date(2015,06,12); function getdateplusdays(origdate, days) { var date = new date(origdate.gettime()); date.setdate(date.getdate() + days); return date; } function getclosestdateinrange(startdate, enddate, date) { return (date < startdate) ? startdate : ((date > enddate) ? enddate: date); } var today = new date(); var range_start = getclosestdateinrange(challenge_start, challenge_end, getdateplusdays(today, -14)); var range_end = getclosestdateinrange(challenge_start, challenge_end, today);
Comments
Post a Comment