3

I have the following javascript:

const to = moment(item.to);
const toNextDay = to.add(1, 'days');

item.to is a string that has the following format:

"2020-06-30T00:00:00"

But is not adding the next day, that would be "2020-07-01T00:00:00"

This is the Function:

  private getCurrentRecord(records: ConfigRecord[]) {
    let result: string = null;
    for (let index = records.length - 1; index >= 0; index--) {
      const item = records[index];

      if (item.from && item.to) {

        const from = moment(item.from);
        const to = moment(item.to).add(1, 'days');
        const today = moment();
        console.log(today.format());
        if (from <= today && today < to) {
          result = item.value;
          break;
        }
      }
    }
    return result;
  }
user3442470
  • 409
  • 2
  • 6
  • 19
  • Can you add to a constant? I'm not sure of this rule in javascript, but I'd try making it non-constant. If this works, please notify me, and I'll 'promote' this comment to an answer. – JosephDoggie Jun 04 '20 at 15:20
  • See also https://stackoverflow.com/questions/563406/add-days-to-javascript-date -- I'd just make the entity a 'var' not a constant … see my comment above – JosephDoggie Jun 04 '20 at 15:23
  • 1
    Hello, I tried using 'let' but tslint from Angular is saying that the value is never reassigned, which is true. Also I tried using var but tslint is telling me this: "Forbidden 'var' keyword, use 'let' or 'const' instead (no-var-keyword)". Also, is not working with var – user3442470 Jun 04 '20 at 15:26
  • 1
    @JosephDoggie `const` just means that the reference itself is immutable, it doesn't say anything about the mutability of the object it refers to. – Thomas Jun 04 '20 at 15:31
  • 1
    Consider using moment's [`isSameOrBefore`](https://momentjs.com/docs/#/query/is-same-or-before/) and [`isBefore`](https://momentjs.com/docs/#/query/is-before/) functions rather than `<=` and `<` operators; that way you can tell it the level of granularity you want (right now it compares down to the millisecond, but you may only care about comparing down to the day). – Heretic Monkey Jun 04 '20 at 15:44
  • Please provide a [mre] showing it not working. Also tell us what time zone you're running the code under, as that can affect the formatted output of the date. – Heretic Monkey Jun 04 '20 at 15:48

3 Answers3

2

Try this one:

const to = moment("2020-06-30T00:00:00");
const toNextDay = moment(to.add(1, 'days').toDate());

As moment is modifying the original moment object, either use toString() or toDate() to get the modified date.

const to = moment("2020-06-30T00:00:00");
const toNextDay = moment(to.add(1, 'days').toDate());
console.log('In local time => ', toNextDay.toString());

const toUTC = moment.utc("2020-06-30T00:00:00");
const toNextDayUTC = moment.utc(toUTC.add(1, 'days').toDate());
console.log('In UTC => ', toNextDayUTC.toString());
<script src="https://momentjs.com/downloads/moment.min.js"></script>
Vivek Patel
  • 1,028
  • 1
  • 10
  • 22
1

Check the rest of the code because this part is correct

const to = moment("2020-06-30T00:00:00")

//undefined

to.format()

//"2020-06-30T00:00:00+02:00"

const nextDay = to.add(1, "day")

//undefined

nextDay.format()

//"2020-07-01T00:00:00+02:00"

to.format()

//"2020-07-01T00:00:00+02:00"

A little warning, Moment.add() mutates the moment so after to.add(1, "day") to and nextDay are the same date, 2020-07-01. Use to.clone().add(1, "day") if you don't want to lose the original moment

Al Hill
  • 479
  • 2
  • 6
  • Unfortunately it does not work for me, eg. `date = "2023-05-27T00:00:00"; const bookingDate = moment(date); const nextDay = bookingDate.clone().add(1, 'days');` Next day shows the same date as `date` – nickornotto May 29 '23 at 22:43
1

Try the following. Parse the string using moment's second argument then use add() to add the specified number of days

var input = "2020-06-30T00:00:00";
let addDay = moment(input, "YYYY-MM-DD hh:mm:ss").add(1, "days");
console.log(addDay.format("YYYY-MM-DDTHH:mm:ss"));
<script src="https://momentjs.com/downloads/moment.js"></script>
joy08
  • 9,004
  • 8
  • 38
  • 73