RegionalSettings.TimeZone.UTCToLocalTime will always return 01/01/0001 00:00:00

john john 961 Reputation points
2020-11-04T18:39:03.983+00:00

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 :-

37400-reg.png

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!!

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,604 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Baker Kong-MSFT 3,801 Reputation points
    2020-11-05T03:17:32.85+00:00

    Hi @john john ,

    The operation has not been submitted, thus it will output the blank date. Please add following snnipet to your code:

    var spTimeZone = context.Web.RegionalSettings.TimeZone;  
    ClientResult<DateTime> cr = spTimeZone.UTCToLocalTime({your date});  
    context.ExecuteQuery();  
      
    Console.WriteLine(cr.Value);  
    

    Best Regards,
    Baker Kong


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.