//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   Alerts
   ASP.NET 2.0
   Classic ASP 1.0
   Databases
      Access DB & ADO
      General SQL Server & Access Articles
      MySQL
      Other Articles
      Schema Tutorials
      Sql Server 2000
      Sql Server 2005
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
databases.aspfaq.com

ASP FAQ Tutorials :: Databases :: General SQL Server & Access Articles :: Can I fix this mm/dd/yyyy <-> dd/mm/yyyy confusion once and for all?


Can I fix this mm/dd/yyyy <-> dd/mm/yyyy confusion once and for all?

This seems to cause a lot of problems. In your environment, you may find that Access or SQL Server is set up for UK dates, and Windows is set up for US dates. Or vice-versa. Or some other weird combination, like SQL Server is set up for UK dates, the SQL machine's Windows is set up for US dates, but the current logged on user has a Danish locale. Combine that with an ASP page on a Windows machine in the next rack, set up for a German charset, and you can see there'll be some big trouble. Take a look at the dates formatted in this example: 
 
<% 
    ReturnDateTime 1033, "English (US)" 
    ReturnDateTime 2057, "English (UK)" 
    ReturnDateTime 3081, "English (Australia)" 
    ReturnDateTime 1031, "German" 
 
    Sub ReturnDateTime(locale, description) 
        Session.LCID = locale 
        Response.Write "<b>" & description & " (LCID = " & locale & ")</b><br>" 
        Response.Write "Long date: " & FormatDateTime(Date, 1) & "<br>" 
        Response.Write "Short date: " & FormatDateTime(Date, 2) & "<p>" 
    End Sub 
%>
 
This results in something like this: 
 
English (US) 
Long date: Monday, February 25, 2002 
Short date: 2/25/2002 
 
English (UK) 
Long date: 25 February 2002 
Short date: 25/02/2002 
 
English (Australia) 
Long date: Monday, 25 February 2002 
Short date: 25/02/2002 
 
German 
Long date: Montag, 25. Februar 2002 
Short date: 25.02.2002
 
So, to display dates to the user in the desired LCID is pretty trivial. Now, to see where the problems really come in with the short date format, try this little script (assuming SQL Server): 
 
<% 
    set conn = CreateObject("ADODB.Connection") 
    conn.open "<connectionString>" 
 
    ' create a simple table 
    conn.execute("CREATE TABLE dt (d DATETIME)") 
 
    ' simple sub to reset locale 
    Sub SetUSLocale() 
        Session.lcid = 1033 
    End Sub 
 
    Call SetUSLocale() 
 
    ' sub for inserting date to DB 
    Sub InsertDateTime(locale, description) 
        session.lcid = locale 
        sql = "INSERT dt VALUES('" & FormatDateTime(date,2) &_ 
            " " & time & "')" 
        on error resume next 
        conn.execute(sql) 
        if err.number <> 0 then 
            response.write description & " failed. " &_ 
                err.description & "<p>" 
            err.clear 
        else 
            response.write description & " passed.<p>" 
        end if 
    End Sub 
 
    ' let's insert current date / time in each locale 
    InsertDateTime 1033, "US English" 
    InsertDateTime 2057, "UK English" 
 
    ' sub for displaying data from database 
    Sub ShowDatabaseDateTime(locale, description, dt) 
        response.write description & " unaltered: " & dt & "<br>" 
        session.lcid = locale 
        response.write description & " altered: " &_ 
            formatdatetime(d,2) & " " & formatdatetime(d,3) & "<p>" 
    End Sub 
 
    set rs = conn.execute("SELECT d FROM dt") 
    do while not rs.eof 
        setUSLocale() 
        d = rs(0) 
        ShowDatabaseDateTime 1033, "US English", d 
        ShowDatabaseDateTime 2057, "UK English", d 
        rs.movenext 
    loop 
 
    rs.close 
    set rs = nothing 
 
    conn.execute("DROP TABLE dt") 
 
    conn.close 
    set conn = nothing 
%>
 
Assuming your SQL Server and Windows machines are set up to accept dates in US format (mm/dd/yyyy), you should see something like the following in the results: 
 
US English passed. 
UK English failed. The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. 
 
US English unaltered: 2/25/2002 9:53:33 PM 
US English altered: 2/25/2002 9:53:33 PM 
 
UK English unaltered: 2/25/2002 9:53:33 PM 
UK English altered: 25/02/2002 21:53:33
 
The UK English insert statement failed because SQL Server doesn't know what month 25 is. In most VB / VBA environments, such as Access, it will implicitly convert 25/02/2002 to 02/25/2002, figuring that the date was entered incorrectly. SQL Server is not so forgiving, and for good reason -- there is ambiguity implied when a database can make decisions for you. How does it know when to draw the line? Should it contemplate changing Nov. 12th to Dec. 11th or vice-versa? Of course not. 
 
To help eliminate differences in international formatting of dates and times, it is recommended to always use YYYYMMDD hh:mm:ss format. Unfortunately, ASP/VBScript doesn't have this format built in, so we have to accommodate a bit. Here is a function I wrote to format a correct database-style date in standard ISO format, regardless of the locale: 
 
<% 
    Function dbDate(dt) 
        dbDate = year(dt) & right("0" & month(dt), 2) &_ 
        right("0" & day(dt),2) & " " & formatdatetime(dt,4) 
    End Function 
%>
 
So, taking the above script, we can correct for differences in the LCID by passing the dates through dbDate() first (note that the only changes in the script are marked in bold): 
 
<% 
    Function dbDate(dt) 
        dbDate = year(dt) & right("0" & month(dt), 2) &_ 
        right("0" & day(dt),2) & " " & formatdatetime(dt,4) 
    End Function
 
 
    set conn = CreateObject("ADODB.Connection") 
    conn.open "<connectionString>" 
 
    ' create a simple table 
    conn.execute("CREATE TABLE dt (d DATETIME)") 
 
    ' simple sub to reset locale 
    Sub SetUSLocale() 
        Session.lcid = 1033 
    End Sub 
 
    Call SetUSLocale() 
 
    ' sub for inserting date to DB 
    Sub InsertDateTime(locale, description) 
        session.lcid = locale 
        sql = "INSERT dt VALUES('" & dbDate(now) & "')" 
        on error resume next 
        conn.execute(sql) 
        if err.number <> 0 then 
            response.write description & " failed. " &_ 
                err.description & "<p>" 
            err.clear 
        else 
            response.write description & " passed.<p>" 
        end if 
    End Sub 
 
    ' let's insert current date / time in each locale 
    InsertDateTime 1033, "US English" 
    InsertDateTime 2057, "UK English" 
 
    ' sub for displaying data from database 
    Sub ShowDatabaseDateTime(locale, description, dt) 
        response.write description & " unaltered: " & dt & "<br>" 
        session.lcid = locale 
        response.write description & " altered: " &_ 
            formatdatetime(d,2) & " " & formatdatetime(d,3) & "<p>" 
    End Sub 
 
    set rs = conn.execute("SELECT d FROM dt") 
    do while not rs.eof 
        setUSLocale() 
        d = rs(0) 
        ShowDatabaseDateTime 1033, "US English", d 
        ShowDatabaseDateTime 2057, "UK English", d 
        rs.movenext 
    loop 
 
    rs.close 
    set rs = nothing 
 
    conn.execute("DROP TABLE dt") 
 
    conn.close 
    set conn = nothing 
%>
 
And the results this corrected script outputs are as follows: 
 
US English passed. 
 
UK English passed. 
 
US English unaltered: 2/25/2002 10:03:00 PM 
US English altered: 2/25/2002 10:03:00 PM 
 
UK English unaltered: 2/25/2002 10:03:00 PM 
UK English altered: 25/02/2002 22:03:00 
 
US English unaltered: 2/25/2002 10:03:00 PM 
US English altered: 2/25/2002 10:03:00 PM 
 
UK English unaltered: 2/25/2002 10:03:00 PM 
UK English altered: 25/02/2002 22:03:00
 
As alternatives to session.LCID, you can try maintaining an LCID in a specific session variable and setting it on a page-by-page basis using @LCID or @CODEPAGE. If you are using Windows Server 2003 and IIS 6.0, you can experiment with the new function Response.Codepage. 
 
For more information on date / time formatting in ASP, please see the following KB articles: 
 
    KB #218964 
    PRB: VBScript Date and Time Formats Change with Logged on User 
 
    KB #229690 
    HOWTO: Set the ASP Locale ID Per the Browser's Language Settings 
 
    KB #264063 
    Date/Time Functions May not be Formatted Properly in Non-English (US) Locales 
 
    KB #271587 
    ASP Pages Use Time/Date Format Based on User That Is Logged On 
 
    KB #306044 
    INFO: Behavior of Date/Time Format Differs When Accessed from ASP 
 
An alternative is to alter the default regional settings for the server, however this may impact other sites / applications and should only be used as a last resort. 
  1. Log on to the server (either physically or through terminal services) and set the correct locale and date format
  2. Open regedit, navigate to the following key, and export it: 
     
    HKEY_CURRENT_USER\Control Panel\International
     
  3. Open the exported file in Notepad, and replace "HKEY_CURRENT_USER" with "HKEY_USERS\.DEFAULT"
  4. Save the file, double-click it, and restart IIS
And finally, in IIS 5.1 and IIS 6.0, there is an application level property called AspLCID. You can see the documentation for this property in the IIS SDK. Also, take a look at these other MSDN Library search results.

Related Articles

Could I get some help with JOINs?
How can I tell which version of MDAC I'm running?
How do I access MIN, MAX, SUM, COUNT values from SQL statements?
How do I change column order in a table structure?
How do I change the order of columns in a table?
How do I concatenate strings from a column into a single row?
How do I convert columns of values into a single list?
How do I determine if a database exists?
How do I document / compare my SQL Server database(s)?
How do I get the IDENTITY / AUTONUMBER value for the row I inserted?
How do I solve 'ADO Could Not Find The Specified Provider'?
Should I use BETWEEN in my database queries?
Why can't I use the * wildcard in a database search?
Why do I get 'Syntax Error in INSERT INTO Statement' with Access?
Why do I get weird results when using both AND and OR in a query?
Why do some SQL strings have an 'N' prefix?
Why does AbsolutePosition return as -1?
Why doesn't SQL Server allow me to separate DATE and TIME?
Why is Query Analyzer only returning 255 characters?
Why should I avoid NULLs in my database?
How do I deal with an apostrophe (') in a SQL statement?

 

 


Created: 2/25/2002 | Last Updated: 5/6/2005 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (113)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...