Friday, 23 January 2015

Get Date Format from Regional Setting of SharePoint 2010 Site programmatically using C#

Scenario:

The requirement was to get the date format for the multilingual web part from the regional setting of a SharePoint site. It had to be achieved programmatically using C# code.

Solution:

DateTime UtcTime = Convert.ToDateTime(DateTime.Now).ToUniversalTime();
            
DateTime Timestamp = SPContext.Current.Web.RegionalSettings.TimeZone.UTCToLocalTime(UtcTime);

SPRegionalSettings RegionSet = SPContext.Current.Web.RegionalSettings;

string Dateformat = System.Globalization.CultureInfo.GetCultureInfo(Convert.ToInt32(RegionSet.LocaleId)).DateTimeFormat.ShortDatePattern;

Label1.Text = Convert.ToString(Timestamp.ToString(Dateformat));

Snippet:





you can comment if you are facing still with any issues.

No comments:

Post a Comment