Modifying the default date-time format for a SharePoint 2007 Site Collection


2008 at 10:29 AM


By default, all my site-collections were using the en-US locale.  This can be seen in site actions --> site settings --> regional settings.  The short date-time format for this locale is "M/d/yyyy" (i.e. 10/24/2008).

I was given the task of trying to figure out how to change the short date-time format for all my site collections to "yyyy-MM-dd" (i.e. 2008-10-24).

My first instinct was to see if there was any settings within SharePoint itself in the Central Admin or Site Settings which allowed me to do this.  No luck.

Next, I figured that SharePoint must get the regional & locale settings from the Regional and Language Options on the server (start --> control panel --> Regional and Language Options), so I went and changed the Short date format here.  FYI, this just sets the values in the registry under HKEY_USERS\.DEFAULT\Control Panel\International.  Again, no luck.

 Customize Regional Option

At this point, I figured I would turn to code and use the CultureAndRegionInfoBuilder class which is new for .NET 2.0. (http://msdn.microsoft.com/en-us/library/system.globalization.cultureandregioninfobuilder(VS.80).aspx).  Here is the code I tried to run on my server (as an administrator):

            try
            {
                CultureAndRegionInfoBuilder.Unregister("en-US");
            }
            catch (System.ArgumentException)
            {
                //The specified custom culture 'en-US' was not found.
                //ignore
            }
            catch (Exception)
            {
                throw;
            }

            CultureAndRegionInfoBuilder carib = new CultureAndRegionInfoBuilder("en-US", CultureAndRegionModifiers.Replacement);

            carib.GregorianDateTimeFormat.ShortDatePattern = "yyyy-M-d";

            carib.Register();

 Again, this didn't seem to work.

Turns out that SharePoint for some reason uses the default short date for the locale it is set at.  So even though I was changing the ShortDatePattern, it wasn't looking at it.

 After a bit of research I found this post  http://sharepointex.blogspot.com/2007/09/how-to-modify-date-format-in-sharepoint.html which gave me the information I needed.

 From the help given in this post, what I decided to do was to try to find a locale/regional setting that had the short date I wanted (fr-CA seems to work) and then use the CultrueAndRegionInfoBuilder to copy all the locale settings from en-US to it.  After this, I changed the locale for all the site collections and their sub-sites to use this customized fr-CA culture which is basically en-US using fr-CA's short date format.  I deployed it as a feature, the main functiona for my code is below:

         public void ChangeLocale(SPSite site)

        {

            try

            {

                CultureAndRegionInfoBuilder.Unregister("fr-CA");

            }

            catch (System.ArgumentException)

            {

                //The specified custom culture 'fr-CA' was not found.

                //ignore

            }

            catch (Exception)

            {

                throw;

            }

 

            CultureAndRegionInfoBuilder carib = new CultureAndRegionInfoBuilder("fr-CA", CultureAndRegionModifiers.Replacement);

            carib.LoadDataFromCultureInfo(new CultureInfo("en-US"));

            carib.LoadDataFromRegionInfo(new RegionInfo("US"));

            carib.GregorianDateTimeFormat.ShortDatePattern = "yyyy-M-d";

            carib.GregorianDateTimeFormat.DateSeparator = "-";

            carib.Register();

 

            CultureInfo ci = new CultureInfo("fr-CA");

 

            using (site)

            {

                for (int i = 0; i < site.AllWebs.Count; i++)

                {

                    using (SPWeb web = site.AllWebs[i])

                    {

                        web.Locale = ci;

                        web.Update();

                    }

                }

            }

        }

        public void RemoveLocale(SPSite site)

        {

            try

            {

                CultureAndRegionInfoBuilder.Unregister("fr-CA");

            }

            catch (System.ArgumentException)

            {

                //The specified custom culture 'fr-CA' was not found.

                //ignore

            }

            catch (Exception)

            {

                throw;

            }

 

            CultureInfo ci = new CultureInfo("en-US");

 

            using (site)

            {

                for (int i = 0; i < site.AllWebs.Count; i++)

                {

                    using (SPWeb web = site.AllWebs[i])

                    {

                        web.Locale = ci;

                        web.Update();

                    }

                }

            }

        }

Note: if you are in a farm environment, this feature needs to be locally activated on all servers so that the custom carib culture can take effect server wide.

Leave a Comment

(required) 
(required) 
(optional)
(required)