Tuesday, September 6, 2011

SharePoint DateTime Control Date Format

If you are working with DateTime Control in SharePoint, you might have a requirement to show date format (For example DD/MM/YYYY) based on the user regional setting instead of US date format (MM/DD/YYYY)

 

You can specify the regional settings locale id to DateTime local id on Page Load

 

int localId = Convert.ToInt32(SPContext.Current.RegionalSettings.LocaleId); 
dtpcStartDate.LocaleId = localId;


If you want to change the default date format, no matter what the user regional setting is, you can do it my writing the custom control by deriving from the Microsoft.SharePoint.WebControls.DateTimeControl.



 

public class CustomDatePicker : Microsoft.SharePoint.WebControls.DateTimeControl
{
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
TextBox tb= (TextBox)this.Controls[0];
DateTime dt = DateTime.Parse(tb.Text);
tb.Text = dt.ToString("dd/MM/yyyy");

}

}
}


Note: For above code, I used the Autopostback = true to trigger OnPreRender event until i figure it out how to trigger onDateChangeed event client side.

0 comments:

Post a Comment