Adding years to date of birth decrements the month by one.

Adding years to date of birth decrements the month by one.

GlyndwrGlyndwr Posts: 128Questions: 35Answers: 1

I am adding eight years to the date of birth; however, when I do this the month decrements by one (i.e., 16/10/2010 + 8 = 16/9/2018 and 16/1/2010 + 8 = 16/0/2018).. I can get round this by adding 1 to the month. However, I am concerned that this may cause other issues. The code is:

              {"data": null, //data is null since we want to access ALL data for the sake of our calculation below
               "render": function(data,type,row) {
                   var DOB = new Date(data.dob);
                   var year = DOB.getFullYear() + 8;
                   var month = DOB.getMonth() + 1;
                   var day = DOB.getDate();
                   return (day + "/" + month + "/" + year);
                   },
              },

Also, I would like the answer returned as dd/mm/yyyy rather than d/m/yyyy.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @Glyndwr ,

    The best bet with anything date/time related is to use Moment.js, it'll make it far easier.

    Cheers,

    Colin

  • GlyndwrGlyndwr Posts: 128Questions: 35Answers: 1

    @colin , this is so much easier:

     var new_date = moment(data.dob, "YYYY-MM-DD").add('years', 8);
    

    Thank you.

This discussion has been closed.