javascript - Calculations on the date of - record dates in the form of a relative -
i need develop code calculate date:
last sunday - last thursday - sunday before last instance. (current day 10-06) 07-06, 04-06, 31-05
last thursday - last sunday - penultimate thursday eg. (current day 05-06) 04-06, 31-05, 28-05
if current day thursday: current thursday - last sunday - last thursday example. (current day 18-06) 18-06, 14-06, 11-06
if current day sunday: current sunday - last thursday - last sunday example. (current day 21-06) 21-06, 18-06, 14-06
just bad calculates these dates shows not should be, last 14-06, showed first date: 14-06, second date 14-06, date of third 11-06 it's not good. today shows (19-06): 14-06, 11-06, 07-06 should 18-06, 14-06, 11-06.
function getlastthursday() { var lt = new date(); lt.setdate(lt.getdate() - lt.getday()-3); // lt = lt.getdate(); return lt; } function getlastsunday() { var ls = new date(); ls.setdate(ls.getdate() - ls.getday()); // ls = ls.getdate(); return ls; } //penultimate thursday function get2lastthursday() { var twolt = new date(); twolt.setdate(twolt.getdate() - twolt.getday()-10); // lt = lt.getdate(); return twolt; } // penultimate saturday function get2lastsunday() { var twols = new date(); twols.setdate(twols.getdate() - twols.getday()-7); // ls = ls.getdate(); return twols; } var dlastthursday = getlastthursday().getdate();//number of day var dlastsunday = getlastsunday().getdate();//number of day var d2lastthursday = get2lastthursday().getdate();//number penultimate day var d2lastsunday = get2lastsunday().getdate();//number penultimate day var mlastthursday = getlastthursday().getmonth()+1; // month number var mlastsunday = getlastsunday().getmonth()+1; // month number var m2lastthursday = get2lastthursday().getmonth()+1; // number penultimate month var m2lastsunday = get2lastsunday().getmonth()+1; // number penultimate month var dforalllastthursday = ('0' + (getlastthursday().getdate())).slice(-2);//to display 0 var mforalllastthursday = ('0' + (getlastthursday().getmonth()+1)).slice(-2); var dforalllastsunday = ('0' + (getlastsunday().getdate())).slice(-2); var mforalllastsunday = ('0' + (getlastsunday().getmonth()+1)).slice(-2); var d2foralllastsunday = ('0' + (get2lastsunday().getdate())).slice(-2); var m2foralllastsunday = ('0' + (get2lastsunday().getmonth()+1)).slice(-2); var d2foralllastthursday = ('0' + (get2lastthursday().getdate())).slice(-2); var m2foralllastthursday = ('0' + (get2lastthursday().getmonth()+1)).slice(-2); var alllastthursday = dforalllastthursday+"-"+mforalllastthursday; // day - month var alllastsunday = dforalllastsunday+"-"+mforalllastsunday; // day - month var all2lastthursday = d2foralllastthursday+"-"+m2foralllastthursday; // day - penultimate month var all2lastsunday = d2foralllastsunday+"-"+m2foralllastsunday; // day - penultimate month var roznica = dlastsunday - dlastthursday; var d = new date(); if(d.getday() == 4 || d.getday() == 0){ var istoday = 1; // confirmation today thursday or sunday var dtoday = d.getdate(); var mtoday = d.getmonth()+1; //adding 0 front need when 1-9 var dforalltoday = ('0' + d.getdate()).slice(-2); var mforalltoday = ('0' + (d.getmonth()+1)).slice(-2); var alltoday = dforalltoday+"-"+mforalltoday; //the problem bad showing how first thursday or sunday of month , last thursday or sunday of month reduce problem if (roznica > 20 || roznica < -20) { if(dlastsday < dlastfday) { lastday = alllastsunday; lastday2 = alllastthursday; } else { lastday = alllastthursday; lastday2 = alllastsunday; } }else if(dlastsunday > dlastthursday){ lastday = alllastsunday; lastday2 = alllastthursday; }else{ lastday = alllastthursday; lastday2 = alllastsunday; } } //the problem bad showing how first thursday or sunday of month , last thursday or sunday of month reduce problem else if (roznica > 20 || roznica < -20) { if(dlastsunday < dlastthursday) { lastday = alllastsunday; lastday2 = alllastthursday; lastday3 = all2lastsunday; } else{ lastday = alllastthursday; lastday2 = alllastsunday; lastday3 = all2lastthursday; } } else if(dlastsunday > dlastthursday) { lastday = alllastsunday; lastday2 = alllastthursday; lastday3 = all2lastsunday; } else { lastday = alllastthursday; lastday2 = alllastsunday; lastday3 = all2lastthursday; } if (typeof alltoday != 'undefined') { firstday = alltoday; secondday = lastday; thridday = lastday2; } else{ firstday = lastday; secondday = lastday2; thridday = lastday3; } console.log(alllastsunday); console.log(alllastthursday); console.log(getlastsunday()); console.log(getlastthursday()); var piecesfirst = firstday.split("-"); var firstcharpiecesfirst = piecesfirst[0].charat(0); var firstcharpiecesfirst2 = piecesfirst[1].charat(0); if (firstcharpiecesfirst=="0") {piecesfirst[0] = piecesfirst[0].charat(1);}; if (firstcharpiecesfirst2=="0") {piecesfirst[1] = piecesfirst[1].charat(1);}; var piecessecond = secondday.split("-"); var firstcharpiecessecond = piecessecond[0].charat(0); var firstcharpiecessecond2 = piecessecond[1].charat(0); if (firstcharpiecessecond=="0") {piecessecond[0] = piecessecond[0].charat(1);}; if (firstcharpiecessecond2=="0") {piecessecond[1] = piecessecond[1].charat(1);}; var piecesthrid = thridday.split("-"); var firstcharpiecesthrid = piecesthrid[0].charat(0); var firstcharpiecesthrid2 = piecesthrid[1].charat(0); if (firstcharpiecesthrid=="0") {piecesthrid[0] = piecesthrid[0].charat(1);}; if (firstcharpiecesthrid2=="0") {piecesthrid[1] = piecesthrid[1].charat(1);};
i use gettime() date calculations , add/subtract miliseconds. has advantage automatically handle things leap years , makes easier check things dst.
Comments
Post a Comment