Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SharePoint中的日期值在检索时休息一天_C#_Datetime_Sharepoint 2013_Csom - Fatal编程技术网

C# 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

我正在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(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)检查字符串
  • 我建议将数据库中的日期时间值存储为UTC。在这种情况下,它是独立于时区的,您将避免未来的冬季/夏季时间转换问题
  • 从数据库检索日期时间时,调用
    ToLocalTime()
    根据服务器时区设置设置时间