0

I have the following code to get the Created date time for a list item:-

((DateTime)item.Created).ToString("dd MMMM yyyy HH:mm:ss") + " )"

now the date time i will get will be in UTC time zone, while our site collection regional setting uses this time zone:-

enter image description here

so if i have a time inside my SharePoint list view = 25/07/2020 11:05 i will get it as 25/07/2020 07:05.. any advice?

2
  • 1
    Check this. let me know if it helps you. Nov 3, 2020 at 14:08
  • @GaneshSanap i already read this article but it uses get_regionalSettings() inside javascript while i am working on CSOM code which does not have such as method
    – John John
    Nov 3, 2020 at 14:32

2 Answers 2

0

This worked for me.

 public static void ConvertUtcToSPRegionalSetting()

        {
           
            string sitrUrl = "http://abc.sharePoint.com/sites/AZV";

            using (var context = new ClientContext(sitrUrl))
            {

                Microsoft.SharePoint.Client.TimeZone sharePointTimeZone = context.Web.RegionalSettings.TimeZone;

                context.Load(sharePointTimeZone);
                context.ExecuteQuery();

                var fixedTimeZoneName = sharePointTimeZone.Description.Replace("and", "&");
                //var timeZoneInfo = TimeZoneInfo.Local;
                
                var easternZone = TimeZoneInfo.GetSystemTimeZones();
                var TimeZoneID = "";
                foreach (var z in easternZone)
                {
                    if (z.DisplayName == fixedTimeZoneName)
                    {
                        TimeZoneID = z.Id;
                        break;
                    }

                }
                
                var myTimeZone = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneID);

                              
                //Destination List
                List oList = context.Web.Lists.GetByTitle("TestAuthor");

                var listItems = oList.GetItems(CamlQuery.CreateAllItemsQuery());

                context.Load(listItems,
                items => items.Include(
                item => item["ReportDate"]
                ));

                context.ExecuteQuery();

                foreach (ListItem item in listItems)
                {
                    DateTime reportDate = (DateTime)item["ReportDate"];
                    
                    var ConvertedDateTime = TimeZoneInfo.ConvertTimeFromUtc(reportDate, myTimeZone);

                    Console.WriteLine(string.Format("Original Datetime: {0}-{1}, Converted TimeZone DateTime:{2}-{3}", reportDate.ToShortDateString(), reportDate.ToShortTimeString(), ConvertedDateTime.ToShortDateString(), ConvertedDateTime.ToShortTimeString()));
                                                                            
                }


            }

        }


0

I had the same issue and I found a solution via the build in helper UTCToLocalTime. I wrote a helper method for that.

  public async Task<DateTime> ConvertToSharePointTimeAsync(ClientContext clientContext, DateTime utcTime)
    {
        Microsoft.SharePoint.Client.TimeZone sharePointTimeZone = clientContext.Web.RegionalSettings.TimeZone;

        var convertedTime = sharePointTimeZone.UTCToLocalTime(utcTime);
        await clientContext.ExecuteQueryRetryAsync();

        return convertedTime.Value;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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