Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Universal Date Time Formatting with C#

Universal Date Time Formatting with C#

Scheduled Pinned Locked Moved C#
databasecsharp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    polishprogrammer
    wrote on last edited by
    #1

    I have an application that needs to record data in a database and the particular date when that data was written to the database. More, I need customers to be able to retrieve data from the database by date or date range. To that end, the MS Access database I'm using has a field called TestDate of type Date/Time, and I set up two date/time pickers to allow customers to select a date or date range from which to get data. All this works well and good until I change my regional/language options so the date format changes, say from mm/dd/yyyy to dd/mm/yyyy. Then, although the MS Access database updates the TestDate data automatically to reflect the change in date/time format and my date/time pickers in the C# appilcation reflect the altered format, the result of executing SQL statements based on the date/time pickers does not return the correct data from the database. Rather, selecting all data from December 10, 2007 retrieves data from October 12, 2007. By contrast, if I select, say November 29, 2007, the correct data is returned. Something, then, is getting confused about ambiguous dates. Any suggestions would be appreciated.

    P 1 Reply Last reply
    0
    • P polishprogrammer

      I have an application that needs to record data in a database and the particular date when that data was written to the database. More, I need customers to be able to retrieve data from the database by date or date range. To that end, the MS Access database I'm using has a field called TestDate of type Date/Time, and I set up two date/time pickers to allow customers to select a date or date range from which to get data. All this works well and good until I change my regional/language options so the date format changes, say from mm/dd/yyyy to dd/mm/yyyy. Then, although the MS Access database updates the TestDate data automatically to reflect the change in date/time format and my date/time pickers in the C# appilcation reflect the altered format, the result of executing SQL statements based on the date/time pickers does not return the correct data from the database. Rather, selecting all data from December 10, 2007 retrieves data from October 12, 2007. By contrast, if I select, say November 29, 2007, the correct data is returned. Something, then, is getting confused about ambiguous dates. Any suggestions would be appreciated.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You are storing using DateTime fields from the textboxes aren't you, rather than using a string? More importantly, you are using a parameter to put the field into the database aren't you? If you want to force a UTC date, then you use the U format qualifier with the DateTime. For example DateTime dt = DateTime.Now; Console.WriteLine(dt.ToString("U"));

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      P 1 Reply Last reply
      0
      • P Pete OHanlon

        You are storing using DateTime fields from the textboxes aren't you, rather than using a string? More importantly, you are using a parameter to put the field into the database aren't you? If you want to force a UTC date, then you use the U format qualifier with the DateTime. For example DateTime dt = DateTime.Now; Console.WriteLine(dt.ToString("U"));

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        P Offline
        P Offline
        polishprogrammer
        wrote on last edited by
        #3

        Yes, I am using a parameterized query to insert new records into the database and storing the data using the DateTime data type rather than a string. Still, somewhere between picking the datetime for the SELECT statement and retrieving the data from the database, the date time format gets lost in translation. If I select a non-ambiguous date such as 13/09/2007 (September 13, 2007), execution of the SQL command returns the correct data, but selecting an ambiguous date, such as 3/12/2007 (intending December 3, 2007) returns erroneous data. The report shows 12/3/2007 (March 12, 2007), but the value of the date prior to the execution of the SQL statement is 3/12/2007 (December 3, 2007).

        R 1 Reply Last reply
        0
        • P polishprogrammer

          Yes, I am using a parameterized query to insert new records into the database and storing the data using the DateTime data type rather than a string. Still, somewhere between picking the datetime for the SELECT statement and retrieving the data from the database, the date time format gets lost in translation. If I select a non-ambiguous date such as 13/09/2007 (September 13, 2007), execution of the SQL command returns the correct data, but selecting an ambiguous date, such as 3/12/2007 (intending December 3, 2007) returns erroneous data. The report shows 12/3/2007 (March 12, 2007), but the value of the date prior to the execution of the SQL statement is 3/12/2007 (December 3, 2007).

          R Offline
          R Offline
          RobScripta
          wrote on last edited by
          #4

          Hi, I'm learning C# by lurking the forums, but have much experience in Access. This solution works in Access, and I think it will work in C# but haven't tested yet. In an Access query you have to enclose the date in # to force american date format. This is how I do this (In Access) Function ScriptaUDF(Datum As Date) ScriptaUDF = "#" & Trim(Str(Month(Datum))) & "/" & Trim(Str(Day(Datum))) & "/" & Trim(Str(Year(Datum))) & "#" End Function So whenever I need a date in a query I use: where (((Table.datefield) = " & ScriptaUDF(Me!Datefield) & ")) I hope this helps

          P 2 Replies Last reply
          0
          • R RobScripta

            Hi, I'm learning C# by lurking the forums, but have much experience in Access. This solution works in Access, and I think it will work in C# but haven't tested yet. In an Access query you have to enclose the date in # to force american date format. This is how I do this (In Access) Function ScriptaUDF(Datum As Date) ScriptaUDF = "#" & Trim(Str(Month(Datum))) & "/" & Trim(Str(Day(Datum))) & "/" & Trim(Str(Year(Datum))) & "#" End Function So whenever I need a date in a query I use: where (((Table.datefield) = " & ScriptaUDF(Me!Datefield) & ")) I hope this helps

            P Offline
            P Offline
            polishprogrammer
            wrote on last edited by
            #5

            That's interesting. The problem I've been having occurs, seemingly, during the execution of the SQL statement. I enclose my SQL SELECT statements with a # sign currently, so "SELECT * FROM Table WHERE Date BETWEEN #" startdate #" AND #" endate" If the # character forces an American date format, that would explain much, I think.

            1 Reply Last reply
            0
            • R RobScripta

              Hi, I'm learning C# by lurking the forums, but have much experience in Access. This solution works in Access, and I think it will work in C# but haven't tested yet. In an Access query you have to enclose the date in # to force american date format. This is how I do this (In Access) Function ScriptaUDF(Datum As Date) ScriptaUDF = "#" & Trim(Str(Month(Datum))) & "/" & Trim(Str(Day(Datum))) & "/" & Trim(Str(Year(Datum))) & "#" End Function So whenever I need a date in a query I use: where (((Table.datefield) = " & ScriptaUDF(Me!Datefield) & ")) I hope this helps

              P Offline
              P Offline
              polishprogrammer
              wrote on last edited by
              #6

              How would I specify a date in a SQL expression without forcing the american date format? Is there something I can use to insert a date in the SQL query without using the # sign? Or, if I use the # sign, can I simply force the format that is needed?

              P 1 Reply Last reply
              0
              • P polishprogrammer

                How would I specify a date in a SQL expression without forcing the american date format? Is there something I can use to insert a date in the SQL query without using the # sign? Or, if I use the # sign, can I simply force the format that is needed?

                P Offline
                P Offline
                polishprogrammer
                wrote on last edited by
                #7

                I found a solution. Basically, the Jet Engine seems to have problems with translation. To get the correct data, I have to do this: SELECT * FROM TABLE WHERE Date BETWEEN Format(#" + startDate + "#,'mm/dd/yyyy')AND Format(#" + endDate + "#,'mm/dd/yyyy') By setting the format in the SQL statement to United States format, the data seems to come back to me correctly.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups