Geeks With Blogs

News My Blog has been moved to
Michael Freidgeim's Blog My Blog has been moved to
I needed to use the same date format in client jQuery UI Datepicker and server ASP.NET code. The actual format can be different for different localization cultures.

I decided to translate Datepicker format to .Net Date format similar as it was asked to do opposite operation in

Note that replace command need to replace whole words and order of calls is important

Function that does opposite operation (translate  .Net Date format toDatepicker format) is described in

/// <summary>
/// Uses regex '\b' as suggested in //
/// </summary>
/// <param name="original"></param>
/// <param name="wordToFind"></param>
/// <param name="replacement"></param>
/// <param name="regexOptions"></param>
/// <returns></returns>
static public string ReplaceWholeWord(this string original, string wordToFind, string replacement,
RegexOptions regexOptions = RegexOptions.None)

string pattern =
String.Format(@"\b{0}\b", wordToFind);
string ret=
Regex.Replace(original, pattern, replacement, regexOptions);
return ret;

/// <summary>
/// E.g "DD, d MM, yy" to ,"dddd, d MMMM, yyyy"
/// </summary>
/// <param name="datePickerFormat"></param>
/// <returns></returns>
/// <remarks>
/// Idea to replace from
///From$.datepicker.formatDate to
///Format a date into a string value with a specified format.
///d - day of month (no leading zero) ---.Net the same
///dd - day of month (two digit) ---.Net the same
///D - day name short ---.Net "ddd"
///DD - day name long ---.Net "dddd"
///m - month of year (no leading zero) ---.Net "M"
///mm - month of year (two digit) ---.Net "MM"
///M - month name short ---.Net "MMM"
///MM - month name long ---.Net "MMMM"
///y - year (two digit) ---.Net "yy"
///yy - year (four digit) ---.Net "yyyy"
/// </remarks>
public static string JQueryDatePickerFormatToDotNetDateFormat(string datePickerFormat)

string sRet = datePickerFormat.ReplaceWholeWord(
"DD", "dddd").ReplaceWholeWord("D", "ddd");
sRet = sRet.ReplaceWholeWord(
"M", "MMM").ReplaceWholeWord("MM", "MMMM").ReplaceWholeWord("m", "M").ReplaceWholeWord("mm", "MM");//order is important
sRet = sRet.ReplaceWholeWord(
"yy", "yyyy").ReplaceWholeWord("y", "yy");//order is important
return sRet;
Posted on Saturday, April 14, 2012 10:40 AM ASP.NET , CSS/DHTML/JavaScript | Back to top

Comments on this post: Translate jQuery UI Datepicker format to .Net Date format

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Michael Freidgeim | Powered by: | Join free