New Years Eve Problem

Discussion in 'JavaScript' started by sarahk, May 27, 2014.

  1. #1
    I'm doing calculations on the end date of a membership, should be a no brainer but I think I'm expecting PHP logic to work in javascript.

    http://jsfiddle.net/q66qc/4/

    I've created a fiddle to avoid having to paste screeds of code here.

    If you select a month and year the field under it will be populated with the date a year from now
    upload_2014-5-28_13-8-10.png

    but if I select January instead of getting New Years Eve (Dec 31) I get this
    upload_2014-5-28_13-8-54.png

    var newEnddate = new Date(startDate.getFullYear(), startDate.getMonth()+12, startDate.getDate());
    newEnddate.setDate(newEnddate.getDate()-1);
    $('#FinanceEnddate').val(newEnddate.getFullYear() + "-" + ('00'+newEnddate.getMonth()).substr(-2,2) + "-" + newEnddate.getDate());
    Code (markup):
     
    Solved! View solution.
    sarahk, May 27, 2014 IP
  2. #2
    A couple things going on here... the month functions are 0 based... January is 0, February is 1 and so on as far as the JavaScript Date() class.

    This is what you want I believe:

    
    function setCardExpiry() {
    
        var startDate = new Date($('#FinanceStartdateYear').val(), $('#FinanceStartdateMonth').val() - 1, 1);
        var newEnddate = new Date(startDate.getFullYear(), startDate.getMonth() + 12, startDate.getDate());
        newEnddate.setDate(newEnddate.getDate() - 1);
    
        $('#enddate').val(newEnddate.getFullYear() + "-" + ('00' + (newEnddate.getMonth() + 1)).substr(-2, 2) + "-" + newEnddate.getDate());
    }
    Code (javascript):
     
    digitalpoint, May 27, 2014 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,830
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #3
    sarahk, May 27, 2014 IP