【问题标题】:Date value in SharePoint is one day off while retrievingSharePoint 中的日期值在检索时休息一天
【发布时间】:2020-06-20 01:42:59
【问题描述】:

我将 DateTime 值保存在 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 列表中正确保存为 截止日期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 列中获取正确的日期值?

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

【问题讨论】:

    标签: c# datetime sharepoint-2013 csom


    【解决方案1】:

    尝试将日期转换为本地时间格式,如下所示

    var PaymentDate = Convert.ToDateTime(item["PaymentDate"].ToString()).ToLocalTime();
    

    【讨论】:

      【解决方案2】:

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

      那么任何使用您网站的人都会知道时间始终在您设置的时区中。

      【讨论】:

        【解决方案3】:
        1. String.IsNullOrEmpty(myVar);检查字符串
        2. 我建议将日期时间值以 UTC 格式存储在 DB 中。在这种情况下,它与时区无关,您将在未来避免冬季/夏季时移问题。
        3. 从数据库中检索日期时间并调用ToLocalTime() 时,会根据您的服务器时区设置设置时间。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多