Iso, Utc Datetime in Sitecore Powershell

Recently, I started exploring Sitecore Powershell Extensions (SPE). It's a masterpiece :).

I have a task of content loading from XML files. I was able to complete the whole logic and loading in less than 4 hours with additional couple of hours for learning SPE.

I struggled a bit while updating the DateTime fields and this post is to share what I have learned. I was initially parsing some 'yyyyMMddHHmmss' format string to datetime in SPE and assigned to a DateField of a news item. Item is updated successfully. But when I see the item in content tree it displays as below.


Non ISO datefield displays 1/1/0001


 After some bing search, tried using raw views as the edit was successful.


non Iso date stored in DateField


I remembered Sitecore stores the time in Utc Iso format from Sitecore 8. So, started using Sitecore.DateUtil static class. This is what I have done


  1. Converted server time to Iso time
  2. Converted Iso time to Utc Iso time
  3. Updated date field with Utc Iso time
And this resolved the issue. Though Sitecore saves the Utc Iso time in DB, Sitecore client will actually display the local server time. So, applications developed in Sitecore must take care of how to handle the Iso Utc date fields for display in their applications.

Iso Utc datefield in Sitecore


Below is simple example to easily understand what happens by using Sitecore.DateUtil class

SPE DateUtil test
SPE DateUtil test

Comments

Post a Comment