question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

isSame() producing strange results

See original GitHub issue

dayjs is making me feel like I’m going insane!

dayjs('2020-10-13T03:30:00.000Z').isSame('2020-10-12T04:53:43.427Z', 'day') // true
dayjs('2020-10-13T03:30:00.000Z').tz('UTC').isSame('2020-10-12T04:53:43.427Z', 'day') //false

Both dates are in UTC, so why is the first query incorrectly saying they’re in the same day?

Does dayjs() assume a local timezone and isSame doesn’t (or vice versa)?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
syntaxyzcommented, May 6, 2022

Have fun.

console.log("Default behaviour:")
const s = dayjs.tz("2022-03-01 15:00", 'Europe/Amsterdam')
const e = dayjs.tz("2022-03-01 15:00", 'Europe/Amsterdam')
console.log("issame?", s.isSame(e)) // false
console.log("isAfter?", s.isAfter(e)) // true
console.log("isBefore?", s.isBefore(e)) // false

dayjs.prototype.isSame = function(that: Date, units: string) {
    const other = dayjs(that).utc()
    return this.utc().startOf(units) <= other && other <= this.utc().endOf(units)
}

dayjs.prototype.isAfter = function(that: Date, units: string) {
    return dayjs(that).utc() < this.utc().startOf(units)
}

dayjs.prototype.isBefore = function(that: Date, units: string) {
    return this.utc().endOf(units) < dayjs(that).utc()
}

console.log("Overridden behaviour")
console.log("issame?", s.isSame(e)) // true
console.log("isAfter?", s.isAfter(e)) // false
console.log("isBefore?", s.isBefore(e)) // false
0reactions
Aloshhcommented, Oct 21, 2021

@iamkun, we are also struggling with this issue. Let me explain my point by giving an illustrative example. We have two dayjs objects, namely: date and breakStartTest, both of them have the the following value 2021-11-17 at 00.00 (local time) or 1637103600000 ms (see query below).

date.isSame(breakStartTest, 'day') //false

Moreover, we are in the CEST timezone. If both dayjs objects assume a local timezone then we expect isSame to return true, meaning that both occur on the same day. However, in our case isSame returns false. Our theory is that the date object (see code above) assumes local time while the breakStartTest object assume UTC. This theory is further confirmed by the query below in which we converted the date object to UTC:

date.utc().isSame(breakStartTest, 'day') //true

We have performed several tests, all of them gave the same result. I have attached an image below to illustrate my point (please excuse me for the artwork)

dayjs

We kindly ask for clarification, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

isSame() function in moment.js or Date Validation
So, I came across this moment.js and interested in knowing how to use isSame(). I tried implementing it but unsuccessful. Like :
Read more >
Strange results from Deseq2
I'm getting strange results from DESeq2 results. I'm comparing 3 samples to another group of 3 samples, which I know is less than...
Read more >
Weird result difference between release and debug even with
Recently I am studying cuda application. But when I use cuda on 2D second partial derivative calculation, it gives result mismatch between ...
Read more >
Is Same - momentjs.com
But if the code runs slower it's possible that the moment created in isSame is measurably after the one created in moment() ,...
Read more >
Averaging a measure over 3 Months - strange results
The Microsoft Matrix doesn't produce any meaningful numbers at all!! I've never had any issues with the Profitbase matrix reporting differently ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found