Question

I have the following CSOM code inside my C# console application:-

var regionalSettings = context.Web.RegionalSettings;
var localDueDate = regionalSettings.TimeZone.UTCToLocalTime(itemversion.Created).Value;

but the localDueDate will always be equal to 01/01/0001 00:00:00.. my site collection regional setting is :-

enter image description here

so i though the above code will add 4 hours to the DateTime.. so can anyone advice what is wrong with my code? in my case the itemversion.Created equal 14/10/2020 13:14:11, but var localDueDate = regionalSettings.TimeZone.UTCToLocalTime(itemversion.Created).Value; will return 01/01/0001 00:00:00!!

Was it helpful?

Solution

01/01/0001 00:00:00 is a blank date. You need to add context.ExecuteQuery() after regionalSettings.TimeZone.UTCToLocalTime(itemversion.Created).Value to submit the convertion operation.

        // timezone
        var spTimeZone = context.Web.RegionalSettings.TimeZone;            

        //context.Load(spTimeZone);            
        //context.ExecuteQuery();

        //Console.WriteLine(spTimeZone.Description);


        var orginaldate = DateTime.ParseExact("14/10/2020 13:14:11","dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);          
       
        ClientResult<DateTime> cr = spTimeZone.UTCToLocalTime(orginaldate);
        context.ExecuteQuery();
        Console.WriteLine(cr.Value);
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top