Translate jQuery UI Datepicker format to .Net Date format

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

///


/// Uses regex '\b' as suggested in //http://stackoverflow.com/questions/6143642/way-to-have-string-replace-only-hit-whole-words
///

///
///
///
///
///
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;
}
///


/// E.g "DD, d MM, yy" to ,"dddd, d MMMM, yyyy"
///

///
///
///
/// 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"
///
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;
}

This article is part of the GWB Archives. Original Author: Michael Freidgeim

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.