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. Web Development
  3. ASP.NET
  4. String was not recognized as a valid DateTime.

String was not recognized as a valid DateTime.

Scheduled Pinned Locked Moved ASP.NET
helpdatabasesysadmin
12 Posts 5 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.
  • V Offline
    V Offline
    vikas shukla
    wrote on last edited by
    #1

    hi sir, i am trying to develop a application.but i have an issue.i have found this error when i am running my application on server.in stead of this when i am running this locally.there is no problem no error.and the database is also on server. when i run on server: when i enter datetime 01/02/2010(dd/MM/yyyy) it's working but when i change this like suppose 17/02/2010(dd/MM/yyyy) then this error occurred.while during save this date i am changing this format to MM/dd/yyyy format by following function. public string ChangeFormat(DateTime dtm, string format) { string str= dtm.ToString(format); return str; } and it's working.it can change the format.but error was same on server. please help me.as soon as possible.

    D R 2 Replies Last reply
    0
    • V vikas shukla

      hi sir, i am trying to develop a application.but i have an issue.i have found this error when i am running my application on server.in stead of this when i am running this locally.there is no problem no error.and the database is also on server. when i run on server: when i enter datetime 01/02/2010(dd/MM/yyyy) it's working but when i change this like suppose 17/02/2010(dd/MM/yyyy) then this error occurred.while during save this date i am changing this format to MM/dd/yyyy format by following function. public string ChangeFormat(DateTime dtm, string format) { string str= dtm.ToString(format); return str; } and it's working.it can change the format.but error was same on server. please help me.as soon as possible.

      D Offline
      D Offline
      Dinesh Mani
      wrote on last edited by
      #2

      When you enter 01/02/2010 the system accepts the date as Jan 02, 2010 i.e. in MM/dd/yyyy format. So when you enter the date as 17/02/2010 it is unable to accept it since 17 is not a valid month.

      V 1 Reply Last reply
      0
      • D Dinesh Mani

        When you enter 01/02/2010 the system accepts the date as Jan 02, 2010 i.e. in MM/dd/yyyy format. So when you enter the date as 17/02/2010 it is unable to accept it since 17 is not a valid month.

        V Offline
        V Offline
        vikas shukla
        wrote on last edited by
        #3

        yes sir, i am agree with you sir.but sir i am changing date to MM/dd/yyyy format.with the help of function.and it is right on locally while on server.it gives that error.

        D 1 Reply Last reply
        0
        • V vikas shukla

          yes sir, i am agree with you sir.but sir i am changing date to MM/dd/yyyy format.with the help of function.and it is right on locally while on server.it gives that error.

          D Offline
          D Offline
          Dinesh Mani
          wrote on last edited by
          #4

          Are pointing to the same database from both your local development system and the server? If not check the date format on the server's database. probably it uses a different date format from your development server.

          V 1 Reply Last reply
          0
          • D Dinesh Mani

            Are pointing to the same database from both your local development system and the server? If not check the date format on the server's database. probably it uses a different date format from your development server.

            V Offline
            V Offline
            vikas shukla
            wrote on last edited by
            #5

            hi sir, i want to convert my dd-MM-yyyy to yyyy-MM-dd. and i am using ToString("yyyy-MM-dd") but it return yyyy-dd-MM. please help me. thanks

            D 1 Reply Last reply
            0
            • V vikas shukla

              hi sir, i want to convert my dd-MM-yyyy to yyyy-MM-dd. and i am using ToString("yyyy-MM-dd") but it return yyyy-dd-MM. please help me. thanks

              D Offline
              D Offline
              Dinesh Mani
              wrote on last edited by
              #6

              Did you check the server's datetime format. The way date is treated depends on the systems' regional settings. Check what is the default date format? dd-MM-yyyy or MM-dd-yyyy

              S 1 Reply Last reply
              0
              • D Dinesh Mani

                Did you check the server's datetime format. The way date is treated depends on the systems' regional settings. Check what is the default date format? dd-MM-yyyy or MM-dd-yyyy

                S Offline
                S Offline
                Subin Mavunkal
                wrote on last edited by
                #7

                Just use split function of string and concatenate it !!!!!!!....I think this may help you.

                D 1 Reply Last reply
                0
                • S Subin Mavunkal

                  Just use split function of string and concatenate it !!!!!!!....I think this may help you.

                  D Offline
                  D Offline
                  Dinesh Mani
                  wrote on last edited by
                  #8

                  I think you need to post the message to the original question. The original author would not have got your reply.

                  V 1 Reply Last reply
                  0
                  • D Dinesh Mani

                    I think you need to post the message to the original question. The original author would not have got your reply.

                    V Offline
                    V Offline
                    vikas shukla
                    wrote on last edited by
                    #9

                    hi sir, actually server datetime format is yyyy-MM-dd so that's why i want to convert my dd-MM-yyyy format to yyyy-MM-dd. and for this i am using str.ToString("yyyy-MM-dd").but this was not working. i am unable to understand why it converts in yyyy-dd-MM. please help me for this conversion. thanks,

                    D M 2 Replies Last reply
                    0
                    • V vikas shukla

                      hi sir, actually server datetime format is yyyy-MM-dd so that's why i want to convert my dd-MM-yyyy format to yyyy-MM-dd. and for this i am using str.ToString("yyyy-MM-dd").but this was not working. i am unable to understand why it converts in yyyy-dd-MM. please help me for this conversion. thanks,

                      D Offline
                      D Offline
                      Dinesh Mani
                      wrote on last edited by
                      #10

                      You have to consider 2 settings here - 1. Date time setting on the IIS server - Affects the format in the appl 2. Date time setting on the Database - Affects the format that is stored in the database. Now, as per our exchanges I understand that your database datetime format is "yyyy-MM-dd" and your application server's date time format is "MM-dd-yyyy". Your application server is expecting you to pass dates in the format "MM-dd-yyyy", whereas you are sending the date in the format "dd-MM-yyyy". That is why you are facing the following issues - 1. When passing 17-02-2010 [Feb 17, 2010] application throws error. 2. When passing 01-02-2010 [Feb 01, 2010] application converts it to 2010-01-02 The trouble here is that your development machine has local date time settings set to "dd-MM-yyyy" whereas your application server has the date time setting set as "MM-dd-yyyy" hence you are having trouble visualizing and replicating the error in development. I would suggest, when you first load the date to your date time variable, you split the text as date, month and year and use the date functions to generate the date based on these inputs instead of passing it as one date value. This would ensure that the date values remain consistent across servers with different date time format settings. Hope this makes sense and helps!! :)

                      1 Reply Last reply
                      0
                      • V vikas shukla

                        hi sir, actually server datetime format is yyyy-MM-dd so that's why i want to convert my dd-MM-yyyy format to yyyy-MM-dd. and for this i am using str.ToString("yyyy-MM-dd").but this was not working. i am unable to understand why it converts in yyyy-dd-MM. please help me for this conversion. thanks,

                        M Offline
                        M Offline
                        m dhu
                        wrote on last edited by
                        #11

                        vikas shukla wrote:

                        str.ToString("yyyy-MM-dd")

                        string format="yyyy-MM-dd";
                        dtm.ToString(format);

                        1 Reply Last reply
                        0
                        • V vikas shukla

                          hi sir, i am trying to develop a application.but i have an issue.i have found this error when i am running my application on server.in stead of this when i am running this locally.there is no problem no error.and the database is also on server. when i run on server: when i enter datetime 01/02/2010(dd/MM/yyyy) it's working but when i change this like suppose 17/02/2010(dd/MM/yyyy) then this error occurred.while during save this date i am changing this format to MM/dd/yyyy format by following function. public string ChangeFormat(DateTime dtm, string format) { string str= dtm.ToString(format); return str; } and it's working.it can change the format.but error was same on server. please help me.as soon as possible.

                          R Offline
                          R Offline
                          R Giskard Reventlov
                          wrote on last edited by
                          #12

                          AS an example try like this to correctly format your dates: Create a web form with 2 labels named lblDateGB and lblDateUS then run the folowing code in the Page Load event:

                          string dateGB = DateTime.Now.ToString();
                          lblDateGB.Text = dateGB;

                          CultureInfo info = new CultureInfo("en-US");
                          DateTime dateUS = DateTime.Parse(dateGB, info);
                          lblDateUS.Text = dateUS.ToString();

                          Note that based on your post the server is defaulted to a US style date so the above should work. The crux is that you are converting the date using a specific culture that matches the server culture so, as long as the original date is a valid date, this should convert it correctly.

                          me, me, me

                          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