0

It is currently Nov 30th 2020 20:36 GMT-6.

I just started getting errors in my test code and found something interesting. After some investigating, I found the Date.Today().Month() in my test code is returning 12 (December) even though it is still 11 (November). I checked my personal time zone and company time zone. Both are GMT-6.

I then tried running system.debug(Date.Today().Month()) in the execute anonymous window and it returned the expected 11 (November).

Any idea why this same function returns two different values depending on where it is executed? Is test code run in UTC time zone? It will be good know for future cases!

3
  • 2
    Is your test apex running under a different user context? – arut Dec 1 '20 at 3:02
  • I believe System.debug always returns in GMT, so but your code might be running in User Context. Can you show us the code, just to understand where and how are you using the date. – Nagendra Singh Dec 1 '20 at 6:01
  • I cannot replicate this. After changed my user timezone, both the test and the anonymous execution respect the new timezone. This was tested Dec 1st 2020 6:00pm GMT+11. However I changed my user timezone (and org's) to GMT-11 (so it is in Nov) for testing. – Chun Dec 1 '20 at 7:12
0

based on your description it seems that in test environment Date() is execute in UTC time zone. GMT-6 Nov 30th 2020 20:36 in UTC time zone is December. Maybe you should retrieve the full Date/Time string in test environment and see if it is acutally in UTC time zone format.

0

I was running the test code in a particular test user context. They were set up in the GMT timezone! Changing this to the correct Chicago timezone fixed it. Thanks all for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.