1

I am saving a DateTime value in one of the field in SharePoint list :

//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}
        }
    }

The Date is correctly saved in SharePoint list as Due Date: 9/30/2017.

Now the issue is when i try to retrieve this Date value :

if (LI[Constants.FieldName_ReqLst_DueDate] != null)
  req.DueDate = (DateTime)LI[Constants.FieldName_ReqLst_DueDate];//Output {9/29/2017 6:30:00 PM}

The output i get here is totally different from that of the value that is saved. How to fetch the correct Date value from a SharPoint DateTime column?

  • 1
    SharePoint saves the dates in UTC string format. While you are retrieving date, you need to convert it as per the the server time zone format. you can get the server timezone from regional setting in site setting. – Ramesh Sep 28 '17 at 8:04
  • Thanks Ramesh. If I do this DateTime.Parse(Convert.ToString(LI[Constants.FieldName_ReqLst_DueDate])).ToLocalTime() it works fine but is it the correct way to do it? – Ishan Sep 28 '17 at 8:15
  • it is okay if all the users are in same time zone. if you have multiple server with different time zone than it may cause an issue. – Ramesh Sep 28 '17 at 8:19
  • Unfortunately its not working on the server. Any alternative? – Ishan Sep 28 '17 at 14:06
3

Use the SPTimeZone.UTCToLocalTime method to convert the UTC date time to local date time.

  • I tried this ClientResult<DateTime> dt= oTimeZone.UTCToLocalTime((DateTime)LI[Constants.FieldName_ReqLst_DueDate]); and i get the output as MinDate i.e {1/1/0001 12:00:00 AM} – Ishan Sep 28 '17 at 14:20
  • Use this DateTime dt=oTimeZone.UTCToLocalTime(DateTime.Parse(LI[Constants.FieldName_Re‌​qLst_DueDate].ToString())); – LZ_MSFT Sep 29 '17 at 1:14
  • i know this is bit old thread, but need to check if some one has done this thing while using rest API. I am having same issue and trying to manipulate using java script. Thanks. – Abhishek Kumar Seth Apr 4 '18 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.