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.