Moment.js date issue for specific formats (/w dashes)

Sadly, dv.date("today") != dv.date("today"), to get the equality operator working you need to use .ts on those dates, like in the following:
$= dv.pages().where( f=> f.date.ts == dv.date( moment().format('yyyy-MM-dd') ).ts ).length


Date handling is icky, very icky. As stated doing dv.date("today") == dv.date("today") returns false. However, you could do dv.date("today").ts == dv.date("today").ts which would return true

When handling dates using dataview they’re of the type DateTime (a Luxon time object), which are indeed not the same as a moment() date object. You could do stuff like dv.date( moment().toISOString() ) to convert the moment date into a DateTime object (but be vary of the time portion of the moment date in question.

So what reliable option do you have?

  • Convert both parts to strings, compare the following:
    • Using dv.date().toFormat("yyyy-MM-dd") (using Luxon tokens)
    • and moment.format("YYYY-MM-DD") (using Moment tokens)
  • Convert to dv.date() (aka DateTime) and use .ts equality comparison
    • Add .ts to the DateTime object: dv.date().ts
    • Strip the time part, and convert to DateTime before adding .ts: dv.date(moment().startOf('day').toISOString()).ts
  • Convert to moment dates, and use .isSame() comparison
    • moment(dv.date("today")).isSame(moment().startOf('day'))
    • It turns out that moment() != moment() also, so you need to use the isSame() comparison even though the date (and timestamp) is the same
An ugly, very ugly test file

Here is the test file showing the return value of various incantation, and comparison of those return values. Read and weep…

[dvDate:: 2023-11-10]

- field value: `$= dv.current().dvDate `
- dv.date > moment > start of day > format: `$= dv.date( moment().startOf('day').format("YYYY-MM-DD") ) `
 - [d] dv.date equality comparison: `$= dv.current().dvDate == dv.date( moment().startOf('day').format("YYYY-MM-DD")) `
   - Or alternatively: `dv.date( moment().format("YYYY-MM-DD") ).ts`
- [p] dv.date.ts equality comparison: `$= dv.current().dvDate.ts == dv.date( moment().startOf('day').format("YYYY-MM-DD")).ts `

#### Reasoning .ts comparison
- [c] dv.date == dv.date: `$= dv.date("today") == dv.date("today") `
- [p] dv.date.ts == dv.date.ts: `$= dv.date("today").ts == dv.date("today").ts `

### Moment comparison
- moment - dv.date: `$= moment(dv.current().dvDate).toISOString() `
- moment - start of day:  `$= moment().startOf('day').toISOString() `
- [c] moment equality comparison: `$= moment(dv.current().dvDate) == moment().startOf('day') `
- [p] moment.isSame comparison: `$= moment(dv.current().dvDate).isSame(moment().startOf('day')) `
- [p] moment.toISOString() comparison:  `$= moment(dv.current().dvDate).toISOString() == moment().startOf('day').toISOString() ` 

### String comparison
- dvDate: `$= dv.current().dvDate.toFormat("yyyy-MM-dd") `
- moment: `$= moment().format("YYYY-MM-DD") `
- [p] String comparison: `$= dv.current().dvDate.toFormat("yyyy-MM-dd") == moment().format("YYYY-MM-DD") `

### TS comparison
- dvDate: `$= dv.current().dvDate.ts `
- moment: `$= dv.date(moment().startOf('day').toISOString()).ts `
- [p] String comparison: `$= dv.current().dvDate.ts == dv.date(moment().startOf('day').toISOString()).ts `

My display of these statements

Using custom checkboxes to indicates which matches [p] and which fails [d], we get this output:

Note especially how many of the displayed values are indeed equal, and yet the comparison is false.

PS! The moment().startOf('day').format("YYYY-MM-DD") is somewhat redundant, as we also use the formatting to loose the time part. Left in there as I’m way too confused already by my own code and date handling…