0

My application heavily relies on time calculation and I read online that GCP App Engine uses UTC time zone. So I made these calculations and on my local dev, the calcTime is totally different than App Engine, App Engine is giving me a big -negative number I'm not sure why??

    const endTime = Math.floor(new Date(filteredTimeLeft).getTime() / 1000);
const convertSec = Number(coin.seconds);
const dateNow = Math.floor(Date.now() / 1000);
const calcTime = endTime - convertSec - dateNow;

Isn't my code suppose to work universally , independent where the server is located at?

justrying
  • 49
  • 4
  • 1
    What is the format of *filteredTimeLeft*? What result are you expecting? What did you get? Perhaps see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Feb 07 '22 at 01:52
  • Format of filteredTimeLeft is Sun, Feb 06, 2022 05:35:36 PM – justrying Feb 07 '22 at 02:00
  • The value of calcTime is different from my local dev and the cloud app engine? why is that? – justrying Feb 07 '22 at 02:01
  • Your local dev is NOT an exact replica of App Engine. You have to run your code on production to see if it's using UTC or not. A better option is for your code to explicitly call UTC – NoCommandLine Feb 07 '22 at 02:08
  • thats what i did here const endTime = moment(filteredTimeLeft, "ddd MMM DD YYYY hh:mm:ss a").utcOffset("-08:00").unix(); const convertSec = Number(coin.seconds); const dateNow = moment().utcOffset("-08:00").unix();; – justrying Feb 07 '22 at 02:38
  • 1
    Extra information should be added to the OP, not just in comments. Moment.js parse tokens "ddd MMM DD YYYY hh:mm:ss a" don't match the input string (commas are missing). If parsed correctly, it will be parsed as local (or perhaps an invalid date), so represents a different moment in time for each host with a different offset. Applying an offset afterward is too late, it's already been parsed. It doesn't change the time value generated from the string, only output values. – RobG Feb 07 '22 at 12:25
  • As proof, `moment().utcOffset("-08:00").unix() === moment().unix()` with about 99.999% certainty (there's a minuscule chance that the system clock ticks between calls to *moment* and results in 1 second discrepancy). – RobG Feb 07 '22 at 12:26
  • ok? how does this help this question tho – justrying Feb 08 '22 at 19:54
  • You may have a look at this [Stackoverflow case](https://stackoverflow.com/questions/24748597/is-it-possible-in-google-app-engine-go-to-format-a-date-using-the-users-local). Let me know if that helps! – Mousumi Roy Feb 26 '22 at 06:56

0 Answers0