0

I have written below method for this but it will fail when the current date will be 31. I need to check if date is 31 it should return me 1st date of next month. Any help would be appreciated

getFutureDateTime: function () {
      var now = new Date();
      var year = now.getFullYear();
      var month = now.getMonth() + 1;
      var day = now.getDate() + 1;// to get current date remove "+1"
      var hour = now.getHours();
      var minute = now.getMinutes();
      var second = now.getSeconds();
      if (month.toString().length == 1) {
           month = '0' + month;
      }
      if (day.toString().length == 1) {
           day = '0' + day;
      }
      if (hour.toString().length == 1) {
           hour = '0' + hour;
      }
      if (minute.toString().length == 1) {
           minute = '0' + minute;
      }
      if (second.toString().length == 1) {
           second = '0' + second;
      }
      var dateTime = year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
      return dateTime;
 },

4 Answers4

1

It looks like you're trying to get the next day as a string. Your best bet is to let the Date object do the rollover between months and years for you, like this:

getFutureDateTime: function () {
    var dt = new Date();
    dt.setDate(dt.getDate() + 1); // Will handle rollover for you
    var year = dt.getFullYear();
    var month = dt.getMonth() + 1;
    var day = dt.getDate();
    var hour = dt.getHours();
    var minute = dt.getMinutes();
    var second = dt.getSeconds();
    if (month.toString().length == 1) {
         month = '0' + month;
    }
    if (day.toString().length == 1) {
         day = '0' + day;
    }
    if (hour.toString().length == 1) {
         hour = '0' + hour;
    }
    if (minute.toString().length == 1) {
         minute = '0' + minute;
    }
    if (second.toString().length == 1) {
         second = '0' + second;
    }
    var dateTime = year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
    return dateTime;
},

Note that if you're doing this in any vaguely modern environment, you can use padStart on the string (and padStart is easily polyfilled):

getFutureDateTime: function () {
    var dt = new Date();
    dt.setDate(dt.getDate() + 1); // Will handle rollover for you
    var dateTime =
        year.toString().padStart(2, "0") +
        "/" +
        month.toString().padStart(2, "0") +
        "/" +
        day.toString().padStart(2, "0") +
        " " +
        hour.toString().padStart(2, "0") +
        ":" +
        minute.toString().padStart(2, "0") +
        ":" +
        second.toString().padStart(2, "0");
    return dateTime;
},

You could give yourself a utility function for the padding, to avoid repeating yourself:

function padZero2(val) {
    return String(val).padStart(2, "0");
}
// ...
getFutureDateTime: function () {
    var dt = new Date();
    dt.setDate(dt.getDate() + 1); // Will handle rollover for you
    var dateTime =
        padZero2(year) +
        "/" +
        padZero2(month) +
        "/" +
        padZero2(day) +
        " " +
        padZero2(hour) +
        ":" +
        padZero2(minute) +
        ":" +
        padZero2(second);
    return dateTime;
},

Similarly, if you use an ES2015 template literal, it may be a bit clearer:

getFutureDateTime: function () {
    const dt = new Date();
    dt.setDate(dt.getDate() + 1); // Will handle rollover for you
    const dateTime = `${padZero2(year)}/${padZero2(month)}/${padZero2(day)} ${padZero2(hour)}:${padZero2(minute)}:${padZero2(second)}`;
    return dateTime;
},
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

You don't need to have that complex function, look at this:

function getFutureDateTime() {
  const regex = /(^[0-9-]+)(t)([^Z.]+)/i;
  const date = new Date();
  const isoFutureDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1).toISOString();
  const matches = iso.match(regex);

  return matches[1] + ' ' + matches[3];
}
0
m= require("moment")

console.log(m().add("months",2).format("YYYY/MM/DD HH:MM:SS"))

use momentjs why to reinvent wheel when you already have some nodejs library for that you can change months to days , years etc to add days,houts,years etc instead of month

https://momentjs.com/guides/#/warnings/add-inverted-param/

PDHide
  • 18,113
  • 2
  • 31
  • 46
-1

You sould probably add an if statement before adding the '0' to test if day==32 => day = 1 and month = month+1


getFutureDateTime: function () {
      var now = new Date();
      var year = now.getFullYear();
      var month = now.getMonth() + 1;
      var day = now.getDate() + 1;// to get current date remove "+1"
      if (day==32){
           day = 1;
           month = month + 1;
      }
      var hour = now.getHours();
      var minute = now.getMinutes();
      var second = now.getSeconds();
      if (month.toString().length == 1) {
           month = '0' + month;
      }
      if (day.toString().length == 1) {
           day = '0' + day;
      }
      if (hour.toString().length == 1) {
           hour = '0' + hour;
      }
      if (minute.toString().length == 1) {
               minute = '0' + minute;
          }
          if (second.toString().length == 1) {
               second = '0' + second;
          }
          var dateTime = year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
          return dateTime;
     },