Sitecore 8 - Date/Time Values Showing Incorrectly, Now Stored As UTC
I encountered this issue when one of my Sitecore instances upgraded from 7.x to 8. A lot of the items make use of Sitecore's date field and is critical to the website i.e. for news and events, etc.
Existing dates were fine (phew!) but I noticed all new date values was showing the day before - now we don't want people turning up to events on the wrong day, so this was a big concern!
After inspecting the raw values of the field, I noticed 26/03/2017 (UK format) was actually being stored as 20170325T230000Z - 1 hour behind. Take note of the "Z" suffix, which denotes a time zone is being used.
The reason for this behaviour is major changes related to the time zones in Sitecore 8. It means that now all the date/time values are stored as UTC time. You can see these field values converted to UTC time if raw values is set, as described above. Most importantly, it is expected behaviour.
Also, the "ServerTimeZone" setting was added to the web.config. Now it is possible to specify the server time zone that the instance uses to convert between UTC time and local time. If the value is blank, Sitecore uses the operating system time zone - which is exactly what I needed it to be.
A useful link: Sitecore Settings supporting UTC implementation
So what is the solution? Well, in my case, the code to render the date on the front-end was handled using the now "old" helper method Sitecore.DateUtil.IsoDateToDateTime
. As Sitecore is storing dates as UTC, I simply had to change this to the new Sitecore 8 method Sitecore.DateUtil.ToServerTime
, which accepts a DateTime object for its parameter, as opposed to a string representation. This correctly handles both ISO and UTC - yay!
There you have it. I hope you find this article useful =)
If you have any questions relating to this blog post, comment below.