Sitecore out of the box provides three date related tokens:
- $date: The system date in yyyyMMdd format
- $time: The system time in HHmmss format
- $now: The system date and time in yyyyMMddTHHmmss format
Server Time Zone : This setting specifies the server time zone that is used by the server to convert UTC time to local time, for example “NZST – New Zealand Standard Time”. If blank, Sitecore uses the operating system time zone.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <settings> <setting name="ServerTimeZone" value="New Zealand Standard Time"/> </settings> </sitecore> </configuration>
Problem: I have a requirement to set the item date field to today’s date (excluding time). We can use Tokens, simple right, but not so fast. We got two options,
- $now: includes current time as well, which is not required.
- $date: adds only date, looks like this suits our requirement. When new items gets created, date field value is replaced by UTC date, not the server date. But as the Server Time Zone is in NZST (UTC+12 or UTC+13, Day light saving), there is 12 or 13 hours difference. So if an item is created in the first 12 or 13 hours of the day, in the UI (Content Editor), it will show yesterday not today.
$date token picking up yesterday’s date
Why the issue: When $date token is used, Sitecore only saves UTC date in the database. Then when it is converts to show in the UI, there is no time part to properly convert back to server date. You can read more here.
Fix: I have created one new custom token, called $serverdate, which takes the Server Time Zone date, converts to UTC and stores it in DB.
$serverdate picks up today’s date
You can find Source code Repo which also includes Sitecore Package: https://github.com/gopigujjula/Foundation.CustomTokens