C# SharePoint中的日期值在检索时休息一天
我正在SharePoint列表中的一个字段中保存日期时间值:C# SharePoint中的日期值在检索时休息一天,c#,datetime,sharepoint-2013,csom,C#,Datetime,Sharepoint 2013,Csom,我正在SharePoint列表中的一个字段中保存日期时间值: //request is an entity that has all he fields in the list, I am only showing DueDate in the below code if (txtdatepicker.Text != String.Empty) { DateTime dueDate; if (DateTime.TryParse(txtdatepic
//request is an entity that has all he fields in the list, I am only showing DueDate in the below code
if (txtdatepicker.Text != String.Empty)
{
DateTime dueDate;
if (DateTime.TryParse(txtdatepicker.Text, out dueDate))
{
request.DueDate = dueDate;//Output {9/30/2017 12:00:00 AM}
}
}
该日期在SharePoint列表中正确保存为到期日:2017年9月30日
现在的问题是,当我尝试检索此日期值时:
if (LI[Constants.FieldName_ReqLst_DueDate] != null)
req.DueDate = (DateTime)LI[Constants.FieldName_ReqLst_DueDate];//Output {9/29/2017 6:30:00 PM}
我在这里得到的输出与保存的值完全不同。如何从SharPoint DateTime列中获取正确的日期值
我尝试了
DateTime.Parse(Convert.ToString)(LI[Constants.FieldName\u ReqLs托洛卡尔先生Time()
可以在本地计算机上正常工作,但如果部署在服务器上,则无法工作。服务器位于何处?如果服务器位于不同的时区,这将影响ToLocalTime()
功能
许多站点将时区与用户配置文件相关联,然后将显示的所有日期/时间转换为该配置值
对于Sharepoint网站,您可以尝试以下操作:
if (LI[Constants.FieldName_ReqLst_DueDate] != null)
{
// create a time zone object hard coding to local time zone
var timeInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var tempDate = (DateTime)LI[Constants.FieldName_ReqLst_DueDate];
req.DueDate = TimeZoneInfo.ConvertTimeFromUtc(tempDate, timeInfo);
}
然后,使用您网站的任何人都会知道时间始终在您设置的时区内。尝试将日期转换为本地时间格式,如下所示
var PaymentDate = Convert.ToDateTime(item["PaymentDate"].ToString()).ToLocalTime();
String.IsNullOrEmpty(myVar)检查字符串代码>
ToLocalTime()
根据服务器时区设置设置时间李>