I have a case where the following code returns a different result depending on if I am debugging the site, or running the code in a unit test:

var start = DateUtil.ToUniversalTime(@event.Start_Date);

Where @event.Start_Date is 12:00PM, December 1, 2030 and the Kind of the date is Unspecified. This is the case regardless of where the code runs. That is, I have confirmed that the input is always the same.

When I run this code in a unit test, start evaluates to 7:00PM, whereas if I run this code while debugging the site, it's 5:00PM.

I suspect that this has to do with how the DateUtil.ToUniversalTime() works:

public static DateTime ToUniversalTime(DateTime serverTime)
    if (serverTime.Kind == DateTimeKind.Utc)
        return serverTime;

    if (serverTime == DateTime.MinValue || serverTime == DateTime.MaxValue)
        return serverTime.SpecifyKind(DateTimeKind.Utc);

    if (serverTime.Kind == DateTimeKind.Local)
        return TimeZoneInfo.ConvertTimeToUtc(serverTime);

    return TimeZoneInfo.ConvertTimeToUtc(serverTime, Settings.ServerTimeZone);

That is, if the Kind of the passed DateTime is Unspecified, the method returns the result of:

TimeZoneInfo.ConvertTimeToUtc(serverTime, Settings.ServerTimeZone)

The key being that it makes a call to Settings.ServerTimeZone.

My Sitecore site uses the Eastern timezone, whereas my local machine uses Mountain timezone, so the delta (2 hours difference) matches up with what I think is going on.

I see two options:

  1. Avoid the use of DateUtil.ToUniversalTime() and instead use TimeZoneInfo.ConvertTimeToUtc(serverTime, Settings.ServerTimeZone) for this specific case
  2. Mock Settings.ServerTimeZone as Eastern time in my tests

One revelation that has come out of this for me is that Sitecore.DateUtil isn't always going to behave as expected when running unit tests, nor any Sitecore util class that reference config settings under the hood.

  • 3
    Does this answer your question? Technique for changing Sitecore configuration for unit tests
    – Mark Cassidy
    Jun 26, 2023 at 23:45
  • So there are several ways to go about mocking changes to settings; the more common ones listed out in the answer I've linked here. That said, unit tests of any kind should not really be concerned with server timezones and time conversion.
    – Mark Cassidy
    Jun 26, 2023 at 23:47
  • Thanks @MarkCassidy. I have updated the question to highlight how it differs from the question you linked to. Jun 30, 2023 at 16:17

2 Answers 2


There is no need in hardcoding setting value in the config. You can specify it directly in the unit test via SettingsSwitcher from the Sitecore.Configuration namespace:

var date = DateTime.Now.SpecifyKind(DateTimeKind.Unspecified);
Console.Out.WriteLine("Date = {0}", DateUtil.ToUniversalTime(date));
using (new SettingsSwitcher("ServerTimeZone", "Eastern Standard Time"))
    Console.Out.WriteLine("Date = {0}", DateUtil.ToUniversalTime(date));


Date = 29.06.2023 11:01:09
Date = 29.06.2023 17:01:09

My analysis proved correct. Some Sitecore util functionality will behave differently in unit tests than when debugging the site because the unit tests don't natively inherit the site's Sitecore config settings. Those values must be fed to the unit tests according to the suggestions listed here.

In this specific case, modifying my app.config of the test project resolved the issue to provide the expected server time zone resolved the issue:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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