1

I have current project where they want a time related deadline inserted into a task list. No matter what I use for the date/time it always seems to be off by and hour or two.

I am in EST using my workstation to run a C# CSOM app to insert items into a tasklist date column.

I have tried using straight DateTime, DateTimeOffset and even UTC strings as input to the timedate column in the list.

Something is converting the time to local, then adjusting it for an hour (or two if its in Daylight savings range).

The code below converts {6/17/2014 12:00:00 PM -06:00} to "2014-06-17T18:00:00.0000000+00:00" When it shows up in SharePoint (O365) It is 6/17/2014 2:00 PM

The region settings for the site are set to CST.

I am losing what little of my mind is left with this one.

        DateTime dt = new DateTime();
        DateTimeOffset dtO = new DateTimeOffset();
        String sTemp = dueDate + " " + timeCst;
        string stempOffset = sTemp + " -6:00";
        if (DateTimeOffset.TryParse(stempOffset, out dtO))
        {
            //it is a valid datetime
            sTemp = dtO.ToUniversalTime().ToString( "o");
        }
        else
        {
            sTemp = sTemp;
        }
        return sTemp;

then I use

    String sUTC = SharePointHelper.convertTimeCst(DueDate, time_CST);
    newItem["DueDate"] = sUTC;
2

When creating the string representation of the date for sending to SharePoint, use the "u" format specifier.

DateTime dt = new DateTime(2014,6,17,13,0,0);
newItem["DueDate"] = dt.ToUniversalTime().ToString("u");
1

I came here looking for a solution to the conversion problems, and the solution for our case was (at the end) quite simple actually:

When reading dates, convert to local time using DateTime.ToLocalTime() When writing dates, convert to utc time using DateTimeTo.ToUniversalTime()

Handling timezones manually is a code smell. I learned my lesson now.

0

We ran into this problem when SPQuerying for recurrences. Our solution was to get the time from the parent SPListItem's DateTime and use the occurrences DateTime only for the date portion.

Hope that helps.

0

Building on Dimos's answer, I just wanted to share what fixed my issue. Basically I was pulling all the items from a Calendar (online using CSOM) and found out that All Day Events have correct times, but Non-All Day were in UTC. This fixed it for me...

(this is powershell but you get the point)

    if ($myEvent["fAllDayEvent"] -eq $false)
    {
        $myDate = ([DateTime]$myEvent["EventDate"]).ToLocalTime()
    }
    else
    {
        $myDate = ([DateTime]$myEvent["EventDate"])
    }

Then a few lines down, I can then compare $myDate (as a date\time) to whatever local data I wish.

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.