[英]Sitecore 8 strange behavior when converting ISO Date to .net DateTime
I have an item with a field " Birthday
". 我有一个带有“
Birthday
”字段的项目。 The value is " 15/03/1977
" and the raw value is " 19770315T000000
". 该值是“
15/03/1977
”和原始值“ 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
": 如果我将字符串格式化为“
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
. 我不知道为什么我有这样的差别,保存日期为
15/03/1977
,输出为14-03-1977
。
And second question is, in above code var date = dateField.DateTime.Date;
第二个问题是,在上面的代码
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. 我将
.Date
分配给变量date
但在输出中,如果没有提供格式,我仍然会看到时间。 I am talking about this lblTest.Text = date.ToString();
我在谈论这个
lblTest.Text = date.ToString();
This may be because of the timezone added to the DateTime field raw values in Sitecore 8. 这可能是因为Sitecore 8中的DateTime字段原始值添加了时区。
Similar problem described in this blog post: Sitecore 8 ServerTimeZone – my dates are all wrong 此博客文章中描述的类似问题: Sitecore 8 ServerTimeZone - 我的日期都错了
The new format contains Z
at the end (eg 20160907T120740Z
) which is part of the ISO 8601 datetime standard for UTC times and indicates that that time is UTC. 新格式最后包含
Z
(例如20160907T120740Z
),它是UTC时间的ISO 8601日期时间标准的一部分,表示该时间是UTC。
I'm not 100% sure if this will help you but you can try: 我不是100%确定这是否会对你有所帮助,但你可以尝试:
Sitecore.DateUtil.ToServerTime(dateField.DateTime).ToString("dd-MM-yyyy");
And second question is, in above code var date = dateField.DateTime.Date;
第二个问题是,在上面的代码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.
我将.Date分配给变量日期,但在输出中,如果没有提供格式,我仍然会看到时间。 I am talking about this lblTest.Text = date.ToString();
我在谈论这个lblTest.Text = date.ToString();
The System.DateTime.Date property is still a DateTime object, just with the default value for the time fragment. System.DateTime.Date属性仍然是DateTime对象,只是具有时间片段的默认值。 You need to apply formatting if you want to lose the time fragment.
如果要丢失时间片段,则需要应用格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.