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. Customized date formating

Customized date formating

Scheduled Pinned Locked Moved C#
helpdatabase
9 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.
  • G Offline
    G Offline
    Glen Harvy 0
    wrote on last edited by
    #1

    Hi, I have always used the following coding when formatting dates for use with SQL: bookingMadeDateTime.ToString("yyyy-MM-dd hh:mm:ss tt") <-- or very similar however I have now been advised by a prospective user that they are using the following formats and the sql insert is failing: Date: dd.MM.yy Long date: dddd, dd' de 'MMMM' de 'yyyy Time: hh:mm tt The error is: There was a syntax error in the date format. [ Expression = 2007-10-29 12:32:33 p.m. ] The dates are entered using a datetime picker and I thought that by formatting the way I have, internationalization would be an issue. Also, the actual expression looks fine to me. Can someone help me with this please. Thanks.

    Glen Harvy

    C P 2 Replies Last reply
    0
    • G Glen Harvy 0

      Hi, I have always used the following coding when formatting dates for use with SQL: bookingMadeDateTime.ToString("yyyy-MM-dd hh:mm:ss tt") <-- or very similar however I have now been advised by a prospective user that they are using the following formats and the sql insert is failing: Date: dd.MM.yy Long date: dddd, dd' de 'MMMM' de 'yyyy Time: hh:mm tt The error is: There was a syntax error in the date format. [ Expression = 2007-10-29 12:32:33 p.m. ] The dates are entered using a datetime picker and I thought that by formatting the way I have, internationalization would be an issue. Also, the actual expression looks fine to me. Can someone help me with this please. Thanks.

      Glen Harvy

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Glen Harvy wrote:

      I have always used the following coding when formatting dates for use with SQL: bookingMadeDateTime.ToString("yyyy-MM-dd hh:mm:ss tt") <-- or very similar however I have now been advised by a prospective user that they are using the following formats and the sql insert is failing

      Why would you be formatting dates for SQL Statements. Surely you would be passing this as a parameter? If you didn't use parameters your code would be highly susceptable to SQL Injection attacks, which would not be good. Please read SQL Injection Attacks and Tips on How to Prevent Them[^] to secure your application. Incidentally it will also solve your current date formatting problem.


      Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

      G 2 Replies Last reply
      0
      • G Glen Harvy 0

        Hi, I have always used the following coding when formatting dates for use with SQL: bookingMadeDateTime.ToString("yyyy-MM-dd hh:mm:ss tt") <-- or very similar however I have now been advised by a prospective user that they are using the following formats and the sql insert is failing: Date: dd.MM.yy Long date: dddd, dd' de 'MMMM' de 'yyyy Time: hh:mm tt The error is: There was a syntax error in the date format. [ Expression = 2007-10-29 12:32:33 p.m. ] The dates are entered using a datetime picker and I thought that by formatting the way I have, internationalization would be an issue. Also, the actual expression looks fine to me. Can someone help me with this please. Thanks.

        Glen Harvy

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

        Always insist on ISO 8601 compliant date and time formats.

        1 Reply Last reply
        0
        • C Colin Angus Mackay

          Glen Harvy wrote:

          I have always used the following coding when formatting dates for use with SQL: bookingMadeDateTime.ToString("yyyy-MM-dd hh:mm:ss tt") <-- or very similar however I have now been advised by a prospective user that they are using the following formats and the sql insert is failing

          Why would you be formatting dates for SQL Statements. Surely you would be passing this as a parameter? If you didn't use parameters your code would be highly susceptable to SQL Injection attacks, which would not be good. Please read SQL Injection Attacks and Tips on How to Prevent Them[^] to secure your application. Incidentally it will also solve your current date formatting problem.


          Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

          G Offline
          G Offline
          Glen Harvy 0
          wrote on last edited by
          #4

          Colin Angus Mackay wrote:

          Why would you be formatting dates for SQL Statements. Surely you would be passing this as a parameter? If you didn't use parameters your code would be highly susceptable to SQL Injection attacks, which would not be good.

          As the input is from a datetime picker and the user has no option but to select a date, I wasn't concerned with injection attacks. I am happy to rewrite the code as you suggest ie paramaterized however I don't see this as solving the problem at hand ie the user hast his computer to use non-US English. Does .Net automatically tanslate date formats?

          Glen Harvy

          C 1 Reply Last reply
          0
          • C Colin Angus Mackay

            Glen Harvy wrote:

            I have always used the following coding when formatting dates for use with SQL: bookingMadeDateTime.ToString("yyyy-MM-dd hh:mm:ss tt") <-- or very similar however I have now been advised by a prospective user that they are using the following formats and the sql insert is failing

            Why would you be formatting dates for SQL Statements. Surely you would be passing this as a parameter? If you didn't use parameters your code would be highly susceptable to SQL Injection attacks, which would not be good. Please read SQL Injection Attacks and Tips on How to Prevent Them[^] to secure your application. Incidentally it will also solve your current date formatting problem.


            Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

            G Offline
            G Offline
            Glen Harvy 0
            wrote on last edited by
            #5

            Further to your suggestion regarding parameters, how do I add this in an Insert Statement. I currently have the following Stingbuilder:

            ....
            commandRecDetails.Append(" VALUES ( '");
            commandRecDetails.Append(bookingMadeDateTime.ToString("yyyy-MM-dd hh:mm:ss tt") + "',");
            commandRecDetails.Append(" " + courtNumber + ", ");
            commandRecDetails.Append(" " + thisBookingNumber.ToString() + ", '");
            etc etc....
            recordBookingDetails.CommandText = commandRecDetails.ToString();

            I can define the paramater

            recordBookingDetails.Parameters.Add("DateMade", bookingMadeDateTime);

            but how do I include it to replace the DateTime value in the string above? Thanks. Glen.

            Glen Harvy

            C 1 Reply Last reply
            0
            • G Glen Harvy 0

              Colin Angus Mackay wrote:

              Why would you be formatting dates for SQL Statements. Surely you would be passing this as a parameter? If you didn't use parameters your code would be highly susceptable to SQL Injection attacks, which would not be good.

              As the input is from a datetime picker and the user has no option but to select a date, I wasn't concerned with injection attacks. I am happy to rewrite the code as you suggest ie paramaterized however I don't see this as solving the problem at hand ie the user hast his computer to use non-US English. Does .Net automatically tanslate date formats?

              Glen Harvy

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              Glen Harvy wrote:

              I am happy to rewrite the code as you suggest ie paramaterized however I don't see this as solving the problem at hand ie the user hast his computer to use non-US English. Does .Net automatically tanslate date formats?

              My PC is set to use Standard English rather than US English and I have no problems. If you use parameters then you don't have to worry about date formats, you just pass a DateTime object in to the parameter and set the SQL type of the parameter to DATETIME or SMALLDATETIME and that's all you need to do.


              Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

              G 1 Reply Last reply
              0
              • G Glen Harvy 0

                Further to your suggestion regarding parameters, how do I add this in an Insert Statement. I currently have the following Stingbuilder:

                ....
                commandRecDetails.Append(" VALUES ( '");
                commandRecDetails.Append(bookingMadeDateTime.ToString("yyyy-MM-dd hh:mm:ss tt") + "',");
                commandRecDetails.Append(" " + courtNumber + ", ");
                commandRecDetails.Append(" " + thisBookingNumber.ToString() + ", '");
                etc etc....
                recordBookingDetails.CommandText = commandRecDetails.ToString();

                I can define the paramater

                recordBookingDetails.Parameters.Add("DateMade", bookingMadeDateTime);

                but how do I include it to replace the DateTime value in the string above? Thanks. Glen.

                Glen Harvy

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                Your SQL String will look like this: ... VALUES(@dateMade, @courtNumber, @thisBookingNumber) everything beginning with an @ is a parameter name. Then add it with recordBookingDetails.Parameters.AddWithValue("@dateMade", bookingMadeDateTime); or recordBookingDetails.Parameters.Add("@dateMade", SqlDbType.DateTime).Value = bookingMadeDateTime;


                Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

                G 1 Reply Last reply
                0
                • C Colin Angus Mackay

                  Glen Harvy wrote:

                  I am happy to rewrite the code as you suggest ie paramaterized however I don't see this as solving the problem at hand ie the user hast his computer to use non-US English. Does .Net automatically tanslate date formats?

                  My PC is set to use Standard English rather than US English and I have no problems. If you use parameters then you don't have to worry about date formats, you just pass a DateTime object in to the parameter and set the SQL type of the parameter to DATETIME or SMALLDATETIME and that's all you need to do.


                  Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

                  G Offline
                  G Offline
                  Glen Harvy 0
                  wrote on last edited by
                  #8

                  Thanks for this confirmation. I have made some changes to the program as you suggest and it certainly seems to be working OK.

                  Glen Harvy

                  1 Reply Last reply
                  0
                  • C Colin Angus Mackay

                    Your SQL String will look like this: ... VALUES(@dateMade, @courtNumber, @thisBookingNumber) everything beginning with an @ is a parameter name. Then add it with recordBookingDetails.Parameters.AddWithValue("@dateMade", bookingMadeDateTime); or recordBookingDetails.Parameters.Add("@dateMade", SqlDbType.DateTime).Value = bookingMadeDateTime;


                    Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

                    G Offline
                    G Offline
                    Glen Harvy 0
                    wrote on last edited by
                    #9

                    It doesn't get any better than this ...:-D Thanks again.

                    Glen Harvy

                    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