0

Using ASPX, is there any way I can parse the short-hand date output of this (in the web.config)...

<globalization culture="en-GB" uiCulture="en-GB" />

...into this JavaScript (part of a jQuery UI Calendar) (in a file named "Exit.aspx")...

 function calender() {
     $('.datepicker').datepicker({ dateFormat: '[output goes here]' });
 }

So, in this case, the date sting output is dd/mm/yy. If I were to change the culture to, say, en-US, it would become mm/dd/yy

2 Answers 2

1

JQuery localization files can be accessed from below mentioned location:

http://nuget.org/packages/jQuery.UI.i18n

1
  • 1
    instead of sharing links it will be better if you explain with an example. Aug 18, 2015 at 11:18
0

First we need to set culture.

This can be done in code in page load:

System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en-GB"); 

or setting the culture at page level using :

<%@ Page UICulture="en-GB" Culture="en-GB" %>

Or like you did with globalization element in web.config:

<globalization uiCulture="en-GB" culture="en-GB" />

Then you page needs this:

System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern

Which we will use your to set it on the control

function calender() {
     $('.datepicker').datepicker({ dateFormat: '<%=System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern%>' });
}

Or using an specific culture:

System.Globalization.CultureInfo.GetCultureInfo("en-GB").DateTimeFormat.ShortDatePattern

There are other options just in case: :

System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern
9
  • Hm, small problem with the first code. It's outputting "dd/MM/yyyy", which, in jQuery, comes out as 01/January/2014.
    – Ducky
    Jan 15, 2014 at 9:59
  • It is because it is based on Thread.CurrentThread.CurrentUICulture
    – Dalorzo
    Jan 15, 2014 at 10:01
  • @Ducky I just tested with web.config and several cultures and it worked fine.
    – Dalorzo
    Jan 15, 2014 at 10:22
  • For some reason, at this end, with the culture set to "en-GB", and using [...].DateTimeFormat.ShortDatePattern%>, it's coming out as ({ dateFormat: 'dd/MM/yyyy' }); in the JS, which is making the datepicker's text box produce "01/January/20142014".
    – Ducky
    Jan 15, 2014 at 10:36
  • Of do you mean year twice?
    – Dalorzo
    Jan 15, 2014 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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