Translate jQuery UI Datepicker format to .Net Date format

Posted by Michael Freidgeim on Geeks with Blogs See other posts from Geeks with Blogs or by Michael Freidgeim
Published on Fri, 13 Apr 2012 23:40:51 GMT Indexed on 2012/04/14 5:30 UTC
Read the original article Hit count: 490

Filed under:
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 http://stackoverflow.com/questions/8531247/jquery-datepickers-dateformat-how-to-integrate-with-net-current-culture-date

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
http://www.codeproject.com/Articles/62031/JQueryUI-Datepicker-in-ASP-NET-MVC


/// <summary>
/// Uses regex '\b' as suggested in //http://stackoverflow.com/questions/6143642/way-to-have-string-replace-only-hit-whole-words
/// </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 http://stackoverflow.com/questions/8531247/jquery-datepickers-dateformat-how-to-integrate-with-net-current-culture-date
///From http://docs.jquery.com/UI/Datepicker/$.datepicker.formatDate to http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
///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;
}

© Geeks with Blogs or respective owner