0

Is it possible that in different Android devices time was measured differently?

  • In the database I save time in UTC
  • in the reactive native application, I save the time as new Date()
  • I COUNT THE DIFFERENCE OF TIME FOR TIMES IN THE SAME MOMENT
  • RESULTS:
    • the emulator is 0 days and 0 hours (OK)
    • the smartphone is -1 days and 23 hours (??)

let date = new Date(dateTimeDone);
var today = new Date();
today.setHours(today.getHours());

seconds = Math.floor((today - (date)) / 1000);
minutes = Math.floor(seconds / 60);
hours = Math.floor(minutes / 60);
days = Math.floor(hours / 24);
hours = hours - (days * 24);
ImpoUserC
  • 599
  • 5
  • 16
  • So what does `dateTimeDone` actually contain? – misorude Nov 05 '18 at 10:05
  • @misorude `dateTimeDone` is the time taken as a string from the server – ImpoUserC Nov 05 '18 at 10:06
  • 1
    "as a string" is not very precise. Is it ISO 8601 (`2018-11-05T19:09:00+09:00`)? Is it something useless like `Nov 05 2018 19:09:00`? Maybe a stringified epoch timestamp, like `1541412540`? User-entered string like `Last tuesday`? (Well, probably not this last one, I'm just saying, "as a string" can mean a lot of things :) ) Also, it can be the case that the smartphone for some reason doesn't sync with NTP, and lost track of time. – Amadan Nov 05 '18 at 10:11
  • Possible duplicate of https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results – misorude Nov 05 '18 at 10:12
  • @Amadan example `2018-11-04 01:24:11.772335` – ImpoUserC Nov 05 '18 at 10:12
  • That has no timezone information. JavaScript interprets it as time wherever the execution environment is set as being (i.e. time zone of the browser). If you intended it to be UTC, you got a bug there. At least append `Z` to it: there's a big difference between `new Date("2018-11-04 01:24:11.772335")` and `new Date("2018-11-04 01:24:11.772335Z")` (unless you happen to test in Britain, I suppose). – Amadan Nov 05 '18 at 10:14
  • Also, I don't think the difference is -1 days and 23 hours - it's because your difference is negative (by subtracting a later date from an earlier), and the way `Math.floor` is not symmetric around zero. – Amadan Nov 05 '18 at 10:18
  • @Amadan In fact, I have different time zones set on devices. I will set the UTC time on the server – ImpoUserC Nov 05 '18 at 10:35
  • Shouldn’t set the time as UTC, could confuse the server. Just make sure thag when you send it (or use it) it is formatted as UTC, and you’re fine. – Amadan Nov 05 '18 at 10:38

0 Answers0