I have an item with a field "Birthday
". The value is "15/03/1977
" and the raw value is "19770315T000000
". I took example from here
Now the output of following code is:
var item = Sitecore.Context.Database.GetItem("{8935CD35-5EAD-4C8E-B85C-E95E9B1ED5C3}");
DateField dateField = (DateField)item.Fields["Birthdate"];
// DateTime object
var date = dateField.DateTime.Date;
// String
lblTest.Text = date.ToString();
Output:
3/14/1977 12:00:00 AM // Instead of 15 here date is 14
If I format the string to "dd-MM-yyyy
":
lblTest.Text = date.ToString("dd-MM-yyyy");
Output
14-03-1977
I don't know why I have this difference, the date saved is 15/03/1977
and the output is 14-03-1977
.
And second question is, in above code var date = dateField.DateTime.Date;
I am assigning .Date
to variable date
but in the output I still see the time if no format is provided. I am talking about this lblTest.Text = date.ToString();
This may be because of the timezone added to the DateTime field raw values in Sitecore 8.
Similar problem described in this blog post: Sitecore 8 ServerTimeZone – my dates are all wrong
The new format contains Z
at the end (e.g. 20160907T120740Z
) which is part of the ISO 8601 datetime standard for UTC times and indicates that that time is UTC.
I'm not 100% sure if this will help you but you can try:
Sitecore.DateUtil.ToServerTime(dateField.DateTime).ToString("dd-MM-yyyy");