Javascript Data Type How to - Add number of month into the given date








Question

We would like to know how to add number of month into the given date.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!-- www . jav a2 s  .c o m-->
add11Months = function (date) {
    var splitDate = date.split("-");
    var newDate = new Date(splitDate[0], splitDate[1] - 1, splitDate[2]);
    newDate.setMonth(newDate.getMonth() + 11);
    splitDate[2] = newDate.getDate();
    splitDate[1] = newDate.getMonth() + 1;
    splitDate[0] = newDate.getFullYear();
    return startdate = splitDate.join("-");
}
var startdate = add11Months("2013-02-01");
document.writeln(startdate)

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: