Dating Game

Posted: 1st July 2001 | Filed under: Press Articles, Technical
Author: Paul Ockenden

This article was originally published in PC Pro and is reproduced with permission.

One of the biggest hassles for any programmer in any language is working with date formats.

Manipulating European-style dates entered into a form and then saving them in the default US format of your database server can be tricky enough, but if it's that bad for you, think about your poor users.

Web sites are global, so forcing all dates to be dd/mm/yy or mm/dd/yy or even yy/mm/dd is bound to upset some visitor some time. Luckily, most modern Web browsers will, when interrogated, return the default language on the visitor's computer, and some sites use this to automatically redirect users to a translated version, if available. But there is another use for this information.

It's a little-known fact that the session object in ASP has a Locale ID property, which you can set yourself: set it to an American Locale and the various date formats will return US format dates. How does this work? Simple enough:

<%
strLang = Request.ServerVariables("HTTP_ACCEPT_LANGUAGE")
posComma = InStr(1, strLang, ",")
If posComma > 0 Then
strLang = Left(strLang, posComma - 1)
End If
select case strLang
case "en-us"
session.LCID = 1033
case "en-gb"
session.LCID = 2057
case "fr"
session.LCID = 1036
case "ja"
session.LCID = 1041
' etc.
End Select
%>

A full list of these Locale IDs can be found at http://msdn.microsoft.com/scripting/vbscript/doc/vsmscLCID.htm. To see the effect of running such code, use a test script such as:

<HTML>
<HEAD>
<TITLE>Locale Tests</TITLE>
</HEAD>
<BODY>
<H1>Date/Time Formats</H1>
<%
' .... Include Locale setting code here
%>
Date : <%=Date()%><BR>
Month : <%=monthname(month(Date()))%><BR>
Time : <%=Time()%><BR>
Currency: <%=FormatCurrency(12.34, 2)%><BR>
Numbers: <%=FormatNumber(1234567,2)%>
</BODY>
</HTML>

All articles Copyright CST Group Limited ©1997-2024

Let's Talk

Whether you have a new or existing project, we’d love to hear from you.

Our experienced team thrive on problem solving and working with your business goals in mind.

Get in touch

Cookie preferences

We'd like to know more about the pages you visit on our site to help improve it. If you're happy for us to collect this information please click accept all. Our full Privacy policy can be found here.