Sharepoint 列表中日期时间字段的日期时间格式
DateTime format for the DateTime field in Sharepoint List
我正在处理需要将日期从一个列表迁移到另一个列表的项目。我的源列表的日期时间格式为 MM/DD/YY,我的目标列表字段的日期时间格式为 DD/MM/YY。当我在目的地解析值时,它的转换不正确。
从列表字段中读取值的代码。
ClientContext ctx = new ClientContext(targetURL);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);
ctx.Load(ctx.Web);
SP.ListCollection listCol = web.Lists;
ctx.Load(listCol);
SP.List list = listCollection.GetByTitle("ListTitle");
SP.CamlQuery camlQuery = new SP.CamlQuery();
camlQuery.ViewXml = "";
SP.ListItemCollection listItemCollection = list.GetItems(camlQuery);
ctx.Load(listItemCollection);
//Execute Query
ctx.ExecuteQuery();
foreach (ListItem _item in listItemCollection)
{
ctx.Load(_item,tmp => tmp.FieldValuesAsHtml);
ctx.ExecuteQuery();
foreach (var fieldValue in _item.FieldValuesAsHtml.FieldValues)
{
string _value = fieldValue.Value; //for datetime field returning value as string
}
}
在目标列表中将 _Value 转换为 Datetime
destItem["Date"]= Convert.ToDateTime(_value);
//date is getting changed as DateTime format is diffrent.
如何获取源列表的日期时间格式?
终于找到答案了。我们可以使用区域设置 属性.
获取日期时间格式
ClientContext.Load(f_Web, website => website.RegionalSettings)
ClientContext.ExecuteQuery();
var format = f_Web.RegionalSettings.DateFormat;
我正在处理需要将日期从一个列表迁移到另一个列表的项目。我的源列表的日期时间格式为 MM/DD/YY,我的目标列表字段的日期时间格式为 DD/MM/YY。当我在目的地解析值时,它的转换不正确。
从列表字段中读取值的代码。
ClientContext ctx = new ClientContext(targetURL);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);
ctx.Load(ctx.Web);
SP.ListCollection listCol = web.Lists;
ctx.Load(listCol);
SP.List list = listCollection.GetByTitle("ListTitle");
SP.CamlQuery camlQuery = new SP.CamlQuery();
camlQuery.ViewXml = "";
SP.ListItemCollection listItemCollection = list.GetItems(camlQuery);
ctx.Load(listItemCollection);
//Execute Query
ctx.ExecuteQuery();
foreach (ListItem _item in listItemCollection)
{
ctx.Load(_item,tmp => tmp.FieldValuesAsHtml);
ctx.ExecuteQuery();
foreach (var fieldValue in _item.FieldValuesAsHtml.FieldValues)
{
string _value = fieldValue.Value; //for datetime field returning value as string
}
}
在目标列表中将 _Value 转换为 Datetime
destItem["Date"]= Convert.ToDateTime(_value);
//date is getting changed as DateTime format is diffrent.
如何获取源列表的日期时间格式?
终于找到答案了。我们可以使用区域设置 属性.
获取日期时间格式ClientContext.Load(f_Web, website => website.RegionalSettings)
ClientContext.ExecuteQuery();
var format = f_Web.RegionalSettings.DateFormat;