Custom DateTimeControl Formatting

A problem that I recently encountered working on a MOSS project was the inability to customize date formating of the Microsoft.SharePoint.WebControls.DateTimeControl for a setting that is outside the realm of the default formatting for a culture. The default culture short date formatting was dd/mm/yyyy for en-AU but instead I needed it to display in dd MMM yyyy. This control has a LocaleId property but no way of simply customizing the formatting of the selected date value.
 
All I could find on the net that would work was to redefine the culture setting in the registry using the CultureAndRegionInfoBuilder (as mentioned here: http://sharepointex.blogspot.com/2007/09/how-to-modify-date-format-in-sharepoint.html). This was not ok for me because I was deploying into a shared environment and this would break someone else’s culture-sensitive formatting. So I created a derived class from the DateTimeControl and overrode the OnPreRender event so that I could handle the formatting myself and this did the trick. Here is my code for doing so:
 

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace TestDerivedControl
{
    [Guid("619b97bb-1c70-426e-b3ec-f76c5324be3a")]
    TestDerivedControl : System.Web.UI.WebControls.WebParts.WebPart
    {
        public TestDerivedControl()
        {
            this.ExportMode = WebPartExportMode.All;
        }
        protected override void Render(HtmlTextWriter writer)
        {
            this.RenderChildren(writer);
        }
       
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            // Add the controls here
            CustomAUDatePicker obj = new CustomAUDatePicker();
           
            obj.ID = "dtcDate";
            obj.AutoPostBack = true;
            obj.DateOnly = true;
            obj.Visible = true;
            obj.SelectedDate = DateTimeNow;
            this.Controls.Add(obj);
        }
    }
    public class CustomAUDatePicker : Microsoft.SharePoint.WebControls.DateTimeControl
    {
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            TextBox box = (TextBox)this.Controls[0];
            DateTime dt = DateTime.Parse(box.Text);
            box.Text = dt.ToString("dd MMM yyyy");
       
        }
          
    }
}
 

6 thoughts on “Custom DateTimeControl Formatting

Add yours

  1. Thank you for you post, it is very helpful to me.But how to use these codes?I think I should build them to a dll file, but how to do as you said "overrode the OnPreRender event"….Sorry for my stupidity~~~Thanks in advance.

  2. In additional, can this code effect the whole SharePoint application?Or only a SharePoint site collection?Thank you agian^^

  3. A little old maybe – but a useful post.
    Just tried to implement the custom time/date format and ran into validation problems. For some reason the validation code fails 😦

  4. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each
    time a comment is added I get three emails with the same comment.
    Is there any way you can remove me from that service?
    Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: