10

I have set the date in the sitecore item for some datetime field as below.

enter image description here

And its getting saved as below format in the master/web database.

20170401T073000Z

When I try to get that value using the below API.

webDB = Sitecore.Data.Database.GetDatabase("web");
var item = webDB.GetItem(itemId);
var dateField = (DateField)item.Fields["fieldName"];
_xxx = Sitecore.DateUtil.IsoDateToDateTime(dateField.Value).ToString();

I am getting the below date and time 4/1/2017 7:30:00 AM

Here the difference is 5.30 hrs.

How do I get the exact time which is showing the CMS?

10

Sitecore stores its date/time values in UTC. And you are 5h30m ahead or behind UTC.

If you'd like to display DateTimes in Server Time:

webDB = Sitecore.Data.Database.GetDatabase("web");
var item = webDB.GetItem(itemId);
var dateField = (DateField)item.Fields["fieldName"];
var serverTime = Sitecore.DateUtil.ToServerTime(dateField.DateTime);
_xxx = Sitecore.DateUtil.FormatShortDateTime(serverTime);

When using Sitecore's ToServerTime method, by default it will use the TimeZone configured in the Operating System. If you would like to override that, you can adjust the ServerTimeZone setting in Sitecore.config:

<setting name="ServerTimeZone" value="Eastern Standard Time"/>

References:

| improve this answer | |
3

The below code is also a solution for this issue:

var dateField = (DateField)item.Fields["fieldname"];
var isoDate = Sitecore.DateUtil.IsoDateToServerTimeIsoDate(dateField.Value);
| improve this answer | |

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.