Dataview - momentjs

timestamp is:

  • 1:00 am - 10:00 pm
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:

  • 1:00 am - 9:00 pm

All the calculation is correct.

You’ve got problems in 3 things: your regex, your MomentJS parser, and your math.

  1. 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 (...).

  1. MomentJS

Your parser has hh when it should have h, and aa when it should have a:

https://momentjs.com/docs/#/displaying/format/

  1. The math:

1am to 10pm is 21 hours, not 23. Or 1260 minutes.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.