0
votes

I am working with a SharePoint Online server in another timezone. I am attempting to retrieve the values from a List with a DateTime field. Using CSOM I am able to retrieve the DateTime value stored in the list, however they are stored with UTC + Daylight savings in the timezone of the SP Server location. How can I change the Timezone of my DateTime object while keeping the value the same? (Example: Change 03:00 PM PST to 03:00 PM EST).

For reference I am already grabbing the SharePoint server TimeZone by using ClientContext.Web.RegionalSettings.TimeZone

Edit1:

Code:

#region Get Sharepoint Server Timezone Offset
var rS = context.Web.RegionalSettings;
context.Load(rS);

var sPtZ = rS.TimeZone;
context.Load(sPtZ);
context.ExecuteQuery();

ReadOnlyCollection<TimeZoneInfo> tZs = TimeZoneInfo.GetSystemTimeZones();
string tZ = rS.TimeZone.Description;

if (tZ.Contains(" and "))
{
    tZ = tZ.Replace(" and ", " & ");
}
#endregion

#region Write List Values to AF
foreach (ListItem item in spCollection)
{
    AFElement well = vendor.Wells.First(x => x.Name == (item.FieldValues["Well_x0020_Name"].ToString()));

    if (well != null)
    {
        DateTime dT = DateTime.Parse(item.FieldValues["efwy"].ToString());
        DateTime utc = TimeZoneInfo.ConvertTimeToUtc(dT, tZs.First(x => x.Id == "UTC")).ToLocalTime();
        AFValue afV = new AFValue((double)item.FieldValues["lbpl"], new AFTime(DateTime.Parse(item.FieldValues["efwy"].ToString()).ToLocalTime()));
        //well.Attributes["Manual Input - Choke Size"].SetValue(afV);
        //well.Database.ApplyChanges();
        //well.Database.CheckIn();
    }
    //Else leave item in list
}
context.ExecuteQuery();
#endregion

rS.Timezone.Description has a value of (UTC-08:00) Pacific Time (US and Canada)

The actual SharePoint List value is 12/3/2019 5:00 AM

My dT value is {12/3/2019 1:00:00 PM} This tells me that SharePoint is returning the value in UTC time.

However, after setting that value as UTC then to local time on DateTime "utc" its showing 7:00AM.

1
After testing, this does not appear to work. editing question to include code for example. - mboyd4546
You can't change the time zone without changing the value. The value of a DateTime is a specific moment in time. The time "03:00 PM PST" did not occur at the same moment as "03:00 PM EST". - Heretic Monkey
The users inputting the data are in CST, we have no control of where our SharePoint Online server is hosted. The data being written to our DB needs to be in CST, yet SharePoint is returning the value in PST (or UTC equivalent of PST). - mboyd4546

1 Answers

0
votes

I have it working, but its not pretty.

DateTime dT = DateTime.Parse(item.FieldValues["efwy"].ToString());
DateTime utc1 = TimeZoneInfo.ConvertTimeToUtc(dT, tZs.First(x => x.Id == "UTC"));
DateTime utc2 = TimeZoneInfo.ConvertTime(utc1, tZs.First(x => x.DisplayName == tZ));
DateTime final = TimeZoneInfo.ConvertTimeToUtc(new DateTime(utc2.Year, utc2.Month, utc2.Day, utc2.Hour, utc2.Minute, utc2.Second, utc2.Millisecond, DateTimeKind.Local)).ToLocalTime();

Any feedback or a cleaner solution would be appreciated. Thanks!