3

I am having a problem with an MVC3 application using a jQuery UI DatePicker object.

Within the MVC application, I ask the user to pick their required culture, i.e en-GB, which then formats all dates and currencies in the application in the british format.

I can then access the formatter code for this via Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern

However, the format returned for UK dates is: dd/MM/yyyy

To format the resulting data from the jQuery UI datepicker, I need to specify a date format. But for UK format dates, I need to specify the format as dd/mm/yy

The simple solution would be to just use Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern in my jQuery calls to format the date, but obviously this gives a formatting inconsistency as this is interpreted differently by jQuery UI (01/December/20112011 instead of 01/12/2011)

Is there any easy way around this?

The only way I can think of doing it is asking the user twice what format to display dates in?

2 Answers 2

0

I could think of another solution, but that's pretty low-tech, I'm not sure if that is the right way to do it:

var dateTime = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
var lowerCaseMonths = Regex.Replace(dateTime, "MM","mm");
var yearOnlyOnce = Regex.Replace(lowerCaseMonths, "yyyy", "yy");

Without "explaining variables" just

Regex.Replace(Regex.Replace(Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern, "MM","mm"), "yyyy", "yy")

Then I guess no matter in what format it is, dd/MM/yyyy MM/DD/yyyy dd-MM-yyyy etc, it should work out in jQuery.

Maybe someone has a better solution, but at least this seems better than asking for user input twice.

1
  • that works great. Hopefully there are no other date formats that are inconsistent however... Commented Dec 7, 2011 at 14:38
0
@System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern.ToLower().Replace("yyyy", "yy")

Not the answer you're looking for? Browse other questions tagged or ask your own question.