xiuix
1
timestamp is:
const times = x.text.match(/^(1[0-2]|0?[0-9]:[0-5][0-9] [AaPp][Mm]) - (1[0-2]|0?[0-9]:[0-5][0-9] [AaPp][Mm])/)
const start = moment(date + " " + times[1], 'YYYYMMDD hh:mm aa')
const end = moment(date + " " + times[2], 'YYYYMMDD hh:mm aa')
const minutes = moment.duration(end.diff(start)).asMinutes()
The minutes should be 1380 minutes (23 hrs), but i’m getting 540 min (9hrs).
However if the timestamp was:
All the calculation is correct.
AlanG
2
You’ve got problems in 3 things: your regex, your MomentJS parser, and your math.
- Regex:
You’ve used a |
to do an ‘OR’, but it’s inside your capturing group (
)
, so it’s really doing:
1[0-2]
or 0?[0-9]:[0-5][0-9] [AaPp][Mm]
What you need is this:
x.text.match(/^((?:1[0-2]|0?[0-9]):[0-5][0-9] [AaPp][Mm]) - ((?:1[0-2]|0?[0-9]):[0-5][0-9] [AaPp][Mm])/)
I’ve used non-capturing “OR” subgroups (?:apple|banana)
inside the two capturing groups (...)
.
- MomentJS
Your parser has hh
when it should have h
, and aa
when it should have a
:
https://momentjs.com/docs/#/displaying/format/
- The math:
1am to 10pm is 21 hours, not 23. Or 1260 minutes.