Pages

Thursday, May 9, 2013

Date columns returning incorrect values in SharePoint object model



I have object model code which is displaying modified date in custom aspx page from SharePoint site, but below simple object model code was returning incorrect value (its adding +4 hours into actual date/time)
e.g.
string strModifiedDt = Convert.ToString(objListItem["Modified"]);

I have found many different links which talks about to change 'regional settings' on web application and site level, I have tried all different options on server level but none of them was working and then I have found this MSDN article which talks about exact issue with accessing date values from object model: http://msdn.microsoft.com/en-us/library/ms197282.aspx

Finally I'm able to resolved and display exact same date/time value as SharePoint site in my custom page from below code and method : DateTime.ToLocalTime()

e.g.
DateTime dtValue = Convert.ToDateTime(objListItem["Modified"]);
strColValue = Convert.ToString(dtValue.ToLocalTime());

Sometime simple things make me crazy :)

1 comment: