5

I have a date field in one of my templates who's default value is $date. When I create an item using that template, the raw value is 20190522Z, but when I view it not raw I see it as yesterday's date (5/21). Likewise it puts it into a bucket structure that's consistent with the actual date \2019\05\22.

I thought I had solved a similar issue before by setting my ServerTimeZone to 700 (Eastern Daylight Time) in my SitecoreSettings.config file, but this didn't seem to fix this particular issue. Is there something else I need to set for the CMS to display it with the correct friendly date?

5
  • 1
    Can you try $now? I suspect it won’t help but is worth a shot.
    – jrap
    May 22, 2019 at 17:00
  • 1
    Sitecore will store all dates as UTC and adjust them to your selected server timezone when displaying.
    – Mark Cassidy
    May 22, 2019 at 17:28
  • @MarkCassidy, but my timezone is set to my correct one. Does that mean I have to set any datefield to something like $date + 4 hours worth of miliseconds or something? May 22, 2019 at 18:51
  • @jrap, actually that did work! May 22, 2019 at 18:55
  • 1
    Interesting. I'm hesitant to add it as an actual answer. "$date: The system date (yyyyMMdd)" vs. "$now: The date and time (yyyyMMddTHHmmss)". I'll do some research first.
    – jrap
    May 22, 2019 at 18:58

2 Answers 2

6

The difference between $date and $now is contained within the Sitecore.Data.ReplaceValues method:

protected virtual string ReplaceValues(string text, Func<string> defaultName, Func<string> defaultId, Func<string> defaultParentName, Func<string> defaultParentId)
{
    if (text.Length == 0 || text.IndexOf('$') < 0)
    return text;
    ReplacerContext context = this.GetContext();
    if (context != null)
    {
    foreach (KeyValuePair<string, string> keyValuePair in (SafeDictionary<string, string>) context.Values)
        text = text.Replace(keyValuePair.Key, keyValuePair.Value);
    }
    text = this.ReplaceWithDefault(text, "$name", defaultName, context);
    text = this.ReplaceWithDefault(text, "$id", defaultId, context);
    text = this.ReplaceWithDefault(text, "$parentid", defaultParentId, context);
    text = this.ReplaceWithDefault(text, "$parentname", defaultParentName, context);
    text = this.ReplaceWithDefault(text, "$date", (Func<string>) (() => DateUtil.IsoNowDate), context);
    text = this.ReplaceWithDefault(text, "$time", (Func<string>) (() => DateUtil.IsoNowTime), context);
    text = this.ReplaceWithDefault(text, "$now", (Func<string>) (() => DateUtil.IsoNow), context);
    return text;
}

Note the key differences:

  • $date
    • nested calls listed below:
    • DateUtil.IsoNowDate = DateTime.UtcNow.ToString("yyyyMMdd") + "Z"
    • Replacement: 20190522Z
  • $now
    • nested calls listed below:
    • DateUtil.IsoNow = DateUtil.ToIsoDate(DateTime.UtcNow) (DateTime.UtcNow = datetime object of UTC time)
    • datetime.ToString("yyyyMMddTHHmmss") + "Z"
    • Replacement: 20190522T122352Z

Which, as the names imply: $date is the UTC "date" string and $now is the UTC "date + time" string.

Setting time zones in Sitecore

https://doc.sitecore.com/developers/90/sitecore-experience-management/en/settings-supporting-utc-implementation.html

Valid time zone values

https://docs.microsoft.com/en-us/previous-versions/windows/embedded/gg154758(v=winembedded.80)

Edit

Richard's answer below explains why the inclusion of the timestamp is important when resolving the proper date. Read his answer in addition to mine.

2
  • How would my server not be set correctly? I've been testing this out on a local instance, so it's going based on my machine's date, no? TimeZone in config files is set to the same Eastern Standard Time... May 22, 2019 at 21:42
  • @LeviWallach, I'll remove that piece. I think Richard Seal nailed the true issue.
    – jrap
    May 23, 2019 at 14:01
6

Based on @jraps research, this is what I believe is happening:

$date is taking the current UTC date and storing that in the DB - if you are in UTC-4 and you create the date when the UTC date has crossed over but your local date has not, then the date will appear to be stored incorrectly. This is because when Sitecore displays that date, there is no time information for it to convert back to your local time.

$now is working, because it is storing time information with the date, so Sitecore can now convert that date+time back to your timezone configured in Sitecore.

I guess $date could be considered a buggy way of storing the date, it would be better if it stored it as 20190522T000000Z, then it could at least be converted back to your configured time zone.

2
  • So I believe that $date is set up as the default value anytime you create a date field in 8.2. So essentially Sitecore should probably change that to $now. Any clue how they set it in 9? May 24, 2019 at 0:43
  • It's odd, this initially fixed the issue (changing it to $now) on both our dev and production servers, but after a new release the production server is showing yesterday's date again. Timezone is set the same in sitecoresettings.config (700), and $now is specified in both the Standard values as well as the default value field for the date field. The odd thing is that if you click on the "today" link after an item is created, the date changes to the correct one, it's just the initial value that is showing yesterday's date - but only on our production server, not our dev server... Sep 20, 2019 at 17:11

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.