dayjs

How do I use strict parsing in dayjs.tz?


Strict parsing is documented for the dayjs() constructor but not for the dayjs.tz() one. Is it possible to enable strict parsing for the dayjs.tz() constructor? If not, is there some way to do it?

Thanks!


Solution

  • According to the Converting to Zone docs, it looks like you can achieve this using the dayjs().tz() instance method (instead of the dayjs.tz() constructor) by setting its second parameter to true.

    On passing a second parameter as true, only the timezone (and offset) is updated, keeping the local time same.

    // parse using strict mode (but not in correct timezone)
    let d = dayjs('2024-01-01', 'YYYY-MM-DD', true);
    
    // update timezone but keep same local time
    d = d.tz("America/Toronto", true);