1

I'm setting some future dates in Javascript like so:

var today = new Date(),
    dd = today.getDate(),
    mm = today.getMonth() + 1, // January is 0
    yyyy = today.getFullYear(),
    mmPlusThree = mm + 3,
    mmPlusFour = mm + 4;

    if (dd < 10) {
        dd = '0' + dd;
    }

    if (mm < 10) {
        mm = '0' + mm;
    }

    var todayDateString = mm + '/' + dd + '/' + yyyy,
        threeMonthDateString = mmPlusThree + '/' + dd + '/' + yyyy,
        fourMonthDateString = mmPlusFour + '/' + dd + '/' + yyyy;

How can I update yyyy for threeMonthDateString or fourMonthDateString? Or is there a better way to get formatted strings (i.e., 12/31/2017) of dates three and four months into the future?

t56k
  • 6,769
  • 9
  • 52
  • 115
  • 1
    I think [this answer](http://stackoverflow.com/a/35894866/6313073) will help you. – Angel Politis Sep 15 '16 at 23:06
  • 1
    have a look at this [fiddle](https://jsfiddle.net/19f94kgt/) - adding months (days, hours etc) is easy if you use a date object, you don't have to worry about "overflow" - it does it all for you - also in that fiddle is a simple function to output date in a string format you require - to avoid repeating the tedious zero fill and add one to month stuff. If you find you are doing a lot of date intensive code, a library like momentjs can take the tediousness out of working with dates - there's even a companion library that handles timezones and daylight savings for you – Jaromanda X Sep 15 '16 at 23:12

0 Answers0