date - Javascript calculate same weekday next year -


i have trouble calculate weekday of given date next year. example: tuesday, 19. april 2016 given date. calculate: tuesday, 18. april 2017. important weekdays same.

my problem leap-years. hope me.

$('.changeyear').click(function() {         var appointments = [];         (var = 1; <= 22; i++) {             var date = $('#tr-fix-tag-' + i).val();             if (date !== "") {                 var splitteddate = date.substr(5).split('.');                 appointments.push(new date(splitteddate[2], splitteddate[1] - 1, splitteddate[0]));             }         }          var counter = 1;         $.each(appointments, function() {             var field = $('#tr-fix-tag-' + counter);             var date = $(this)[0];             var newdate = new date(date.setfullyear(date.getfullyear() + 1));              if (isschaltjahr(newdate.getfullyear()))                 field.val(formatdate(new date(newdate.setdate(newdate.getdate() - 2))));             else                 field.val(formatdate(new date(newdate.setdate(newdate.getdate() - 1))));              counter++;         });     }); 

if want add year , keep day same, can adjust resulting date many days required nearest date of same day, e.g.

function samedaynextyear(d) {    // copy date    var t = new date(+d);    // current day number    var startdaynum = d.getday();    // add 1 year    t.setfullyear(t.getfullyear() + 1);    // move closest day same day number    var diff = startdaynum - t.getday();    // if diff more 3 days, need subtract rather add    diff = (diff > 3) ? diff-7 : diff;    t.setdate(t.getdate() + diff);    return t;    }    [new date(2016, 1, 13),  // sat 13 feb 2016 - sat 11 feb 2017   new date(2016, 1, 5),   // fri 05 feb 2016 - fri 03 feb 2017   new date(2016, 1, 6),   // sat 06 feb 2016 - sat 04 feb 2017   new date(2016, 1, 29),  // mon 29 feb 2016 - mon 27 feb 2017   new date(2016, 2, 1)]   // tue 01 mar 2016 - tue 28 feb 2017   .foreach(function(d) {    document.write(d + '<br>' + samedaynextyear(d) + '<br><br>');  });

the above work day in year, leap or otherwise.

however, dates means go next or previous month (e.g. 2016-03-01 goes 2017-02-28). if don't want happen, can compare start , end months , either add or subtract week original month (so 2017-03-07).


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -