gpt4 book ai didi

sharepoint - MOSS 2007 : SPListItem. DateTime 字段的 GetFormattedValue 有错误?

转载 作者:行者123 更新时间:2023-12-04 01:30:45 25 4
gpt4 key购买 nike

SPListItem.GetFormattedValue 似乎对 DateTime 字段有一个奇怪的行为。
它通过 SPListItem 的索引器检索 DateTime 值,根据此 MSDN article返回本地时间。
这是 Reflector 的片段

public string GetFormattedValue(string fieldName)
{
SPField field = this.Fields.GetField(fieldName);
if (field != null)
{
return field.GetFieldValueAsHtml(this[fieldName]);
}
return null;
}

所以它使用 SPListItem 的索引器来检索值,而不是 SPFields.GetFieldValueAsHtml 来格式化值。 GetFieldValueAsHtml 似乎假设日期是 UTC 并将其转换为本地时间,无论它是什么类型。 (反射器显示它使用 GetFieldValueAsText,它使用 value.ToString() 但由于某种原因,它假定时间为 UTC。)

最终结果是通过 listItem.GetFormattedValue() 获得的时间字段上的字符串表示(至少在我的情况下)是不正确的,是本地时间 +(本地时间 - UTC)。

有没有人遇到过与 SPListItem.GetFormattedValue() 相同的问题,您的解决方法是什么?

最佳答案

在调用 GetFieldValueAsHtml 之前将日期转换回通用时间工作得很好。

DateTime localTime = (DateTime)item["DueDate"];
// this is local time but if you do localDateTime.Kind it returns Unspecified
// treats the date as universal time..
// let's give it the universal time :)
DateTime universalTime = SPContext.Current.Web
.RegionalSettings.TimeZone.LocalTimeToUTC(localTime);
string correctFormattedValue =
item.Fields["DueDate"].GetFieldValueAsHtml(universalTime);

关于sharepoint - MOSS 2007 : SPListItem. DateTime 字段的 GetFormattedValue 有错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/277640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com