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?

  • 1
    Check this. let me know if it helps you. – Ganesh Sanap Nov 3 '20 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 '20 at 14:32
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()));
                                                                            
                }


            }

        }


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.