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.

Time/Date Diff is Inaccurate.

See original GitHub issue

So we are using ISO Strings to log timestamps in our DB for various different things, and one thing we need to do is take the local server date/time, and then a date/time from our database and differentiate it and get the time between them.

So we did something like this;

const getTimeRemaining = enddate => {
  const end = DateTime.fromISO(enddate);
  const start = DateTime.local();
  const finalDate = end.diff(start, ['days', 'hours']).toObject();

  if (finalDate.days !== 0) {
    return `${finalDate.days} Days Left`;
  }
  return `${finalDate.hours} Hours Left`;
};

Now this is functional, but the time returned will always be 1 hour ahead. For example, our server time is; Dec 29 03:55:23 The end date in our database is; 2018-12-29T03:50:00.000Z.

So this should now be returning a negative since the duration has gone past the end date, but for some odd reason we instead get this 0.9165527777777778 Hours Left, so it’s adding an additional hour for no reason. (That I can see at least).

If we swap the diff around so it’s const finalDate = start.diff(end, ['days', 'hours']).toObject();

This fixes the issue, but then obviously all values are negative. I can just strip the negative value from the strings I suppose, but this doesn’t feel like the best way I should be doing things.

Hopefully you guys can let me know what is going wrong here.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
maggiepintcommented, Dec 29, 2018

Hi,

If I were guessing, I would say you are somewhere just east of GMT - perhaps Amsterdam, Berlin, Paris or Lagos, where there is a 1 hour offset from UTC right now 😃. The problem you have here is that your database has a UTC date (as designated by the Z at the end of the value), but you are comparing it to a local date. Having switched my computer to Berlin time for fun, I can break your problem down a bit.

Your end date is 2018-12-29T03:50:00.000Z, but DateTime.fromISO() will change dates it parses from UTC to local by default. Thus, upon parse you get this:

luxon.DateTime.fromISO('2018-12-29T03:50:00.000Z').toString()
"2018-12-29T04:50:00.000+01:00"

If that date really is in UTC, then once converted to local time, it is in fact .91 hours AHEAD of your start time, which is presumably also in local time - as you are using .local() to generate it.

0reactions
marbusercommented, Dec 30, 2018

For most use cases, storing the date in UTC is the right thing to do. I’m suggesting you set your server to UTC too. But more immediately, I’ll just repeat that if the database time is right, your code is working as expected.

Perhaps working as intended for the package but definitely not intended in my use-case. I’m currently testing storing the values with the offset like so; 2018-12-30T16:00:27.425+01:00

Hopefully this fixes it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL Server : DATEDIFF not accurate - Stack Overflow
Datediff counts when date boundaries( month ) crossed. You need to be careful when using Datediff . – Pரதீப். Nov 6, 2015 at...
Read more >
datediff is giving wrong calculations in some cases
I have a datediff calculation as below. It gives correct calculation in most cases. But in some cases it is giving wrong results...
Read more >
Excel 2016 DATEDIF function returns wrong value for elapsed ...
I am trying to calculate the elapsed time down to the day between two dates but the day seems to be calculated from...
Read more >
DATEDIFF() Returns Wrong Results in SQL Server? Read This.
So in a nutshell, your results could look “wrong” for any datepart depending on the dates/times. Your results can look extra wrong when...
Read more >
DateTimeInterface::diff - Manual - PHP
Return Values ¶. The DateInterval object represents the difference between the two dates. The return value more specifically represents the clock-time interval ...
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