2017-09-28 29 views
0

我在外地的一个在SharePoint列表保存DateTime值:日期为1天关闭,而检索

//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列表作为截止日期9/30/2017

现在的问题是,当我尝试检索此日期值:

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列中获取正确的Date值?

我试了DateTime.Parse(Convert.ToString(LI[Constants.FieldName_ReqLs‌​t_DueDate])).ToLocal‌​Time()在本地机器上正常工作,但如果部署在服务器上,那么它不起作用。

回答

0

服务器位于何处?如果服务器位于不同的时区,则会影响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); 
} 

然后使用你的网站就知道了时间的人总是在你设置时区。