locked
how to add year in datepicker RRS feed

  • Question

  • User-1634604574 posted

    i have this date i want to add year by 60 number and get error calculation

    <input type="text" id="date_of_birth" value="2019-01-01">
    <input type="text" id=retiredate>
    
    <script>
    
    
            var d = new Date();
            d.setMonth(d.getMonth() - 1);
            d.setFullYear(d.getFullYear() + 60)
            d.setHours(0, 0, 0);
            d.setMilliseconds(0);
                
            $("#date_of_joining").datepicker("setDate", d);
    </script>
    
    i get this retire date 2079-06-25  which is error must be 2079-01-01
    
    

    Thursday, July 25, 2019 8:56 AM

All replies

  • User753101303 posted

    Hi,

    So it seems you want to force as well both the day and month ie :

     d.setHours(0,0,0,0);
     d.setDate(1); // want to force 1st ?
     d.setMonth(0); // want to force January ?
     d.setFullYear(d.getFullYear() + 60);

    Edit: or building the date you want rather than changing values : var d = new Date(new Date().getFullYear()+60,0);

    For now the problem is that you don't do anything with the day and you decrease the month by 1 when it seems you want January 1st ?

    Thursday, July 25, 2019 9:21 AM
  • User-1634604574 posted

    i want to calculate retirement date 

    Thursday, July 25, 2019 9:33 AM
  • User753101303 posted

    Ok and ? The js above should give the result you asked for. If this is not what you actually want tell us more.

    Thursday, July 25, 2019 10:46 AM
  • User-719153870 posted

    Hi zhyanadil.it@gmail.com,

    The d.getMonth() in your js will get the month of today(2019.7.25).

    First, you need to assign a value to d. Please refer:

    var d = new Date(document.getElementById('date_of_birth').value);

    Then, change the setMonth method to:

    d.setMonth(d.getMonth());

    Best Regard,

    Yang Shen

    Friday, July 26, 2019 2:30 AM