Introduction
Currently I’m working on creating tool to get the delta date in a site collection; SharePoint 2010.
Problem Background
Initially I started trying to get the modified date of Last Item in a list using list.LastItemModifiedDate. Here the list is a SPList. But the actual date time of last modified item was different from the date I got programatically.
Here is the code I used to get the last item modified date.
foreach (SPWeb spWeb in sPWebFromWebCollection) { SPListCollection listCollection = spWeb.Lists; foreach (SPList list in listCollection) { DateTime lastModifiedDate= list.LastItemModifiedDate; Console.WriteLine(lastModifiedDate.ToString()); } }
The out put was: 6/27/2013 8:34:25 AM
But 6/27/2013 2:04:25 PM is the date which appeared in the user interface of the last modified list.
Solution Background
It is because list.LastItemModifiedDate returns UTC DateTime, not a local one; but in the UI the local time is appeared. So what we have to do it convert UTC Date Time to local time.
Solution
Do do this, call RegionalSettings.TimeZone.UTCToLocalTime(). So that you can get the local time; the same time which appears in the list user interface.
Here is the code.
foreach (SPWeb spWeb in sPWebFromWebCollection) { SPListCollection listCollection = spWeb.Lists; foreach (SPList list in listCollection) { DateTime lastModifiedDate= list.LastItemModifiedDate; Console.WriteLine(lastModifiedDate.ToString()); Console.WriteLine(spWeb.RegionalSettings.TimeZone.UTCToLocalTime(lastModifiedDate)); } }
Conclusion
Don’t get confused because both list.LastItemModifiedDate and the date appears in list are different. Its the matter in time zone.
Perfect. Was bit confused with the difference in the time. You saved my time.
ReplyDelete