moment-range icon indicating copy to clipboard operation
moment-range copied to clipboard

ISO 8601 time interval parsed wrong

Open StefanKern opened this issue 5 years ago • 2 comments

From the wiki we get the following different formats to devine a time interval

There are four ways to express a time interval:

  1. Start and end, such as "2007-03-01T13:00:00Z/2008-05-11T15:30:00Z"
  2. Start and duration, such as "2007-03-01T13:00:00Z/P1Y2M10DT2H30M"
  3. Duration and end, such as "P1Y2M10DT2H30M/2008-05-11T15:30:00Z"
  4. Duration only, such as "P1Y2M10DT2H30M", with additional context

I tried to parse this formats with moment.range(...) but only the first interval is parsed correctly. These are my testcyases

'2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
moment.range('2007-03-01T13:00:00Z/2008-05-11T15:30:00Z').contains(moment("2007-03-02")), // true
moment.range('2007-03-01T13:00:00Z/2008-05-11T15:30:00Z').contains(moment("2007-03-01")), // false
moment.range('2007-03-01T13:00:00Z/2008-05-11T15:30:00Z').contains(moment("2008-05-12")), // false
"2007-03-01T13:00:00Z/P1Y2M10DT2H30M",
moment.range('2007-03-01T13:00:00Z/P1Y2M10DT2H30M').contains(moment("2007-03-02")), // false --> should be TURE!
moment.range('2007-03-01T13:00:00Z/P1Y2M10DT2H30M').contains(moment("2007-03-01")), // false
moment.range('2007-03-01T13:00:00Z/P1Y2M10DT2H30M').contains(moment("2008-05-12")),
"P1Y2M10DT2H30M/2008-05-11T15:30:00Z",
moment.range('P1Y2M10DT2H30M/2008-05-11T15:30:00Z').contains(moment("2007-03-02")), // false --> should be TURE!
moment.range('P1Y2M10DT2H30M/2008-05-11T15:30:00Z').contains(moment("2007-03-01")), // false
moment.range('P1Y2M10DT2H30M/2008-05-11T15:30:00Z').contains(moment("2008-05-12")), // false
"P1Y2M10DT2H30M",
moment.range('P1Y2M10DT2H30M').contains(moment("2007-03-02")), // false --> should be TURE!
moment.range('P1Y2M10DT2H30M').contains(moment("2007-03-01")), // false
moment.range('P1Y2M10DT2H30M').contains(moment("2008-05-12")), // false

Demo: https://stackblitz.com/edit/moment-js-ttpb3c?file=src%2Fapp%2Fapp.component.ts

This means that only the first case is supported, but other cases, which are in the standard, are not supported.

StefanKern avatar Feb 21 '20 14:02 StefanKern

I got this problem too.

And i found PR 259 had been merged. https://github.com/rotaready/moment-range/pull/259

Before release a new version you can implement it in your project. It is easy to implement base on moment.duration (moment version large then v2.3?)

xyy94813 avatar Dec 26 '20 05:12 xyy94813

This is still an issue in version 4.0.2

lissau avatar Jul 28 '22 12:07 lissau