Search code examples
millisecondsluxon

Luxon - set milliseconds for toISO()


I'm using the following to get the end of day for a date coming from a date picker:

var date = DateTime.fromISO('2018-05-05').endOf('day');

What I want to end up with is

"2018-05-05T23:59:59+02:00"

however, I cannot get rid of the milliseconds:

console.log(date.toISO({suppressMilliseconds: true}));
// => outputs "2018-05-05T23:59:59.999+02:00"

Is there a more elegant way to do this besides simply setting the millisecond to 0:

date.c.millisecond = 0;
console.log(date.toISO({suppressMilliseconds: true}));
// => outputs "2018-05-05T23:59:59+02:00"

Solution

  • Right, suppressMilliseconds only applies if they're 0. (See here).

    But there's a much easier way to round down the second:

    DateTime.fromISO('2018-05-05')
      .endOf('day')
      .startOf('second')
      .toISO({ suppressMilliseconds: true })
    

    You should never mutate the Luxon object like in your workaround.