When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Tuesday, April 10, 2001

Working with Databases and International Date Formats
By Darren Neimke


Workaround for Non-English Countries
Darren's article works wonders for international, English-speaking countries, like England, the United States, Australia, and so on. However, in non-English speaking countries, the months of the year are spelled differently and, hence, have different abbreviations. If you are from a non-English speaking country you may wish to read Giuliano Sauro's article: Using the ISO Date Format for International Dates.

- continued -

For general answers to your date/time questions, be sure to check out the Date and Times FAQ Category on ASPFAQs.com!

If you've ever written a script that needs to UPDATE or INSERT Date values in a database and you live outside of North America, then I'd nearly bet that (like me) you've stayed up until midnight cursing the fact that all dates in SQL Server and MS-Access are natively converted from US date format (mm/dd/yyyy).

Why is that a problem I hear you ask? Simple, I'll tell you the answer to that question on 6/4/2001. Did you see that? No? OK then, what is 6/4/2001? Well if you are converting dates using mm/dd/yyyy format then the answer would be the 4th-Jun-2001. On the other hand if you were converting dates using dd/mm/yyyy format (as is the case in Australia and the UK) then the answer would be 6th-Apr-2001. To see what I mean, if we copy this code into an ASP page and run it:

'English - United States LCID
Session.LCID = 1033
Response.Write Date() & "<br />"

'English - Australia LCID
Session.LCID = 3081
Response.Write Date() & "<br />" 

the results are:

4/10/2001
10/04/2001

For more information on using Session.LCID to alter locale-specific information, be sure to read: Using the Locale Identifier (LCID). You can view a list of all of the valid LCIDs here.

Now, before I explain how to remedy the problem, which, incidentally I already have anyway ;), first let's try to get a better understanding of what exactly is happening. Let's say that we produce an SQL String which looks strikingly like this:

Dim strSQLUpdate
' If you're using SQL Server...
strSQLUpdate = "UPDATE tblOrders SET fldDateOrdered = '" & Date() & "' " & _
               "WHERE fldStatus = open ;"

' If you're using MS-Access...
strSQLUpdate = "UPDATE tblOrders SET fldDateOrdered = #" & Date() & "# " & _
               "WHERE fldStatus = open ;" 

The VBScripting engine interprets the Date() function as a value, formats it based on your Server/LCID settings, and then packages it all up as a *String* to send off to the database's Query Engine. Once the Query Engine recieves this SQL statement it goes about the job of Analyzing and Optimizing the query before passing it on the the Storage engine to actually file the information away in the database.

To see why this is a problem we simply need to understand that all dates are stored in the database as numbers. In order to be able to store a date as a number, the date has to be converted to something other than the standard calendar format. The numeric representation of dates is called a Julian, or Serial, date. To do this, the date is converted to an offset from a fixed point in time.

In the case of Microsoft Access, this offset is 30th-Dec-1899, and all dates are stored as the number of days since this date. Thus 7/7/93 is stored as 34157, meaning 34,157 days since 30th-Dec-1899. Negative numbers represent dates prior to 30th-Dec-1899.

Since adding 1 to a date represents 1 day or 24 hours, each hour is stored as .041666..., or 1/24 of a day. In Microsoft Access all times are stored as a fraction of a day. Each hour is 1/24 of a day, each minute 1/1440, each second 1/86400. So 3:00 is stored as .125 (or 1/8 of a day), and 16:00 is stored as 0.666..., (or 2/3 of a day). Conversely, 0.2 represents 4:48 hours (1/5 of a day), and so on.

Therefore we see that the following snippet produces a result of 28/02/1900 6:00:00 AM (assuming your LCID is set to display dates in dd-mm-yyyy format):

Response.Write CDate( CDbl(60.25) )

This is because 28th-Feb-1900 is 60 days added to 30-Dec-1899, and 06:00 AM is obviously a quarter (or .25) of the way through the day itself. The general point here is that the underlying value stored in the database is simply a DOUBLE.

Now that we know how a database physically stores a date (answer: as a DOUBLE), we're ready for Part 2, in which we'll look at how the Query Engine converts the SQL string containing the date (which is a string itself) into the proper format.

  • Read Part 2!


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
    Microsoft Article: 7.0, Microsoft's Lucky Version?
    Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Windows Server 2008
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES