Sitecore- why are my dates all wrong?!

The short answer – Sitecore changed something and you didn’t read all the small print in the release notes (see ‘Time zones and UTC’).

When upgrading Sitecore to 8.0 or higher you may have noticed that dates and times are displaying incorrectly. Or if you were unlucky enough to upgrade in the winter you may have only started to notice this problem as you entered Daylight Savings Time (DST).

In Sitecore 8 the way DateTime values are stored changed from the exact DateTime value you selected to storing the UTC equivalent of what you selected.

Old format (raw value):

20160907T130740

New format (raw value):

20160907T120740Z

The ‘Z’ is the way you can distinguish which values have been entered in the new format. Z is part of the ISO 8601 datetime standard for UTC times and indicates that that time is UTC.

This is not a bad thing until you realise that everywhere you are displaying dates and times on your website you are outputting the date something like:

    DateField dtf = (DateField)Sitecore.Context.Item.Fields["Date"]; 
    if (dtf != null) 
    { 
        Response.Write(dtf.DateTime.ToString("dd MMM yyyy")); 
    } 


This will take the raw UTC value of the field and display it as it is which is not how you entered it in the CMS. The solution is simple enough, you just need to use the ToServerTime Sitecore helper to convert the DateTime values.

For example the above code would be replaced with:

    DateField dtf = (DateField)Sitecore.Context.Item.Fields["Date"]; 
    if (dtf != null) 
    { 
        Response.Write(Sitecore.DateUtil.ToServerTime(dtf.DateTime).ToString("dd MMM yyyy")); 
    } 


Just ensure that you update all necessary references including where Date or DateTime comparisons occur.

If you are lucky you were only displaying dates using the Sitecore UI controls (e.g. <sc:Date … />) which will of course handle the date adjustment for you.

More information on Sitecore date/time best practices.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s