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. Visual Basic
  4. can someone please help me to convert a string to time?... tnx!!!

can someone please help me to convert a string to time?... tnx!!!

Scheduled Pinned Locked Moved Visual Basic
helpcsharpsql-serverquestion
8 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.
  • C Offline
    C Offline
    cutequencher
    wrote on last edited by
    #1

    i have a string format "18:15:15"(fetch from a dataset) i want to convert it to time format.. but i cant... ive tried to concatinate the time to "1/1/1900 "&"18:15:15", and convert it using the ff: assume that datevalue = "1/1/1900 "&"18:15:15" Convert.ToDateTime(datevalue) ctype(datevalue,datetime) date.parse(datevalue) but still im recieving an error message : String is not recognized as a valid datetime... please help me... i am using vb.net 2002 and im fetching data from mssql 200 tnx in advance!!!

    C T 2 Replies Last reply
    0
    • C cutequencher

      i have a string format "18:15:15"(fetch from a dataset) i want to convert it to time format.. but i cant... ive tried to concatinate the time to "1/1/1900 "&"18:15:15", and convert it using the ff: assume that datevalue = "1/1/1900 "&"18:15:15" Convert.ToDateTime(datevalue) ctype(datevalue,datetime) date.parse(datevalue) but still im recieving an error message : String is not recognized as a valid datetime... please help me... i am using vb.net 2002 and im fetching data from mssql 200 tnx in advance!!!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Why are you storing the times as strings in SQL Server ? Seems to me that you can just call the split method on your string, and use int.tryparse to get out the hours, minutes, seconds.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      1 Reply Last reply
      0
      • C cutequencher

        i have a string format "18:15:15"(fetch from a dataset) i want to convert it to time format.. but i cant... ive tried to concatinate the time to "1/1/1900 "&"18:15:15", and convert it using the ff: assume that datevalue = "1/1/1900 "&"18:15:15" Convert.ToDateTime(datevalue) ctype(datevalue,datetime) date.parse(datevalue) but still im recieving an error message : String is not recognized as a valid datetime... please help me... i am using vb.net 2002 and im fetching data from mssql 200 tnx in advance!!!

        T Offline
        T Offline
        TwoFaced
        wrote on last edited by
        #3

        date.parse("18:15:15") worked for me but I'm using VS 2005. If that really won't work you might try Date.ParseExact if that's available to you. It would look something like this.

        Dim t As Date = Date.ParseExact("18:15:15", "HH:mm:ss", Globalization.DateTimeFormatInfo.InvariantInfo)

        If that fails you could fall back to the split function which has already been suggested.

        C 1 Reply Last reply
        0
        • T TwoFaced

          date.parse("18:15:15") worked for me but I'm using VS 2005. If that really won't work you might try Date.ParseExact if that's available to you. It would look something like this.

          Dim t As Date = Date.ParseExact("18:15:15", "HH:mm:ss", Globalization.DateTimeFormatInfo.InvariantInfo)

          If that fails you could fall back to the split function which has already been suggested.

          C Offline
          C Offline
          cutequencher
          wrote on last edited by
          #4

          it still doesnt work... maybe i havent supplied you an enough information... here's my code... please help me... Private ds_schedule As New DataSet() Dim strSQL As String Dim Connect As New SqlConnection(ConnectionString) strSQL = "select t.tload_id,t.date_value,convert(char(10),t.start_time,14) start_time,convert(char(10),t.end_time,8) end_time,upper(r.last_name)+', '+r.first_name teacher_name,secondary_teacher_id,datediff(hh,t.start_time,t.end_time)*t.rate totla_rate,t.remarks FROM dbo.ttcpCustomizedSchedule t left outer join dbo.ResourcePersons r on r.teacher_id=t.teacher_id where t.tload_id = " & tload_id.ToString & "order by date_value" Dim data_adapter As SqlDataAdapter data_adapter = New SqlDataAdapter(strSQL, Connect) data_adapter.TableMappings.Add("Table", "ttcpcustomizedschedule") data_adapter.Fill(ds_schedule) dtgCustomSched.SetDataBinding(ds_schedule, "ttcpcustomizedschedule") Dim dateValue As Date Dim startdate As Date Dim enddate As Date row = dtgCustomSched.CurrentRowIndex dateValue = dtgCustomSched.Item(row, 1) startdate = Date.ParseExact(dtgCustomSched.Item(row, 2), "HH:mm:ss", Globalization.DateTimeFormatInfo.InvariantInfo) enddate = Date.ParseExact(dtgCustomSched.Item(row, 3), "HH:mm:ss", Globalization.DateTimeFormatInfo.InvariantInfo) ConnectionString==my connection that is working properly dtgCustomSched==the name of my datagrid ds_schedule==the name of my dataset dbo.ttcpCustomizedSchedule = table name i think ther must be something wrong with the interaction of the dataset,datgrid,and data adapter... but i dont know what it is... thanx!

          T 1 Reply Last reply
          0
          • C cutequencher

            it still doesnt work... maybe i havent supplied you an enough information... here's my code... please help me... Private ds_schedule As New DataSet() Dim strSQL As String Dim Connect As New SqlConnection(ConnectionString) strSQL = "select t.tload_id,t.date_value,convert(char(10),t.start_time,14) start_time,convert(char(10),t.end_time,8) end_time,upper(r.last_name)+', '+r.first_name teacher_name,secondary_teacher_id,datediff(hh,t.start_time,t.end_time)*t.rate totla_rate,t.remarks FROM dbo.ttcpCustomizedSchedule t left outer join dbo.ResourcePersons r on r.teacher_id=t.teacher_id where t.tload_id = " & tload_id.ToString & "order by date_value" Dim data_adapter As SqlDataAdapter data_adapter = New SqlDataAdapter(strSQL, Connect) data_adapter.TableMappings.Add("Table", "ttcpcustomizedschedule") data_adapter.Fill(ds_schedule) dtgCustomSched.SetDataBinding(ds_schedule, "ttcpcustomizedschedule") Dim dateValue As Date Dim startdate As Date Dim enddate As Date row = dtgCustomSched.CurrentRowIndex dateValue = dtgCustomSched.Item(row, 1) startdate = Date.ParseExact(dtgCustomSched.Item(row, 2), "HH:mm:ss", Globalization.DateTimeFormatInfo.InvariantInfo) enddate = Date.ParseExact(dtgCustomSched.Item(row, 3), "HH:mm:ss", Globalization.DateTimeFormatInfo.InvariantInfo) ConnectionString==my connection that is working properly dtgCustomSched==the name of my datagrid ds_schedule==the name of my dataset dbo.ttcpCustomizedSchedule = table name i think ther must be something wrong with the interaction of the dataset,datgrid,and data adapter... but i dont know what it is... thanx!

            T Offline
            T Offline
            TwoFaced
            wrote on last edited by
            #5

            What error are you getting? What line throws the error? (Assuming the problem is with ParseExact) Have you tried stepping into your code and verified dtgCustomSched.Item(row,2) is returning what you expect? Also note that the format of the string must be like this "03:22:12" NOT "3:22:12". If you don't use leading zero's then you need to change the formating string. "H:m:s" will allow for optional leading zeros.

            C 2 Replies Last reply
            0
            • T TwoFaced

              What error are you getting? What line throws the error? (Assuming the problem is with ParseExact) Have you tried stepping into your code and verified dtgCustomSched.Item(row,2) is returning what you expect? Also note that the format of the string must be like this "03:22:12" NOT "3:22:12". If you don't use leading zero's then you need to change the formating string. "H:m:s" will allow for optional leading zeros.

              C Offline
              C Offline
              cutequencher
              wrote on last edited by
              #6

              i already check on those things.. but still, im getting the same error... dtgCustomSched.Item(row,2) is returning what you expect? == gives me the right string with leading zeros

              1 Reply Last reply
              0
              • T TwoFaced

                What error are you getting? What line throws the error? (Assuming the problem is with ParseExact) Have you tried stepping into your code and verified dtgCustomSched.Item(row,2) is returning what you expect? Also note that the format of the string must be like this "03:22:12" NOT "3:22:12". If you don't use leading zero's then you need to change the formating string. "H:m:s" will allow for optional leading zeros.

                C Offline
                C Offline
                cutequencher
                wrote on last edited by
                #7

                What error are you getting? == String is not recognized as a valid datetime What line throws the error? == startdate=Date.ParseExact(dtgCustomSched.Item(row, 2), "HH:mm:ss" i already used the "H:m:s" format but still, nothing happened. thanx in advance!

                T 1 Reply Last reply
                0
                • C cutequencher

                  What error are you getting? == String is not recognized as a valid datetime What line throws the error? == startdate=Date.ParseExact(dtgCustomSched.Item(row, 2), "HH:mm:ss" i already used the "H:m:s" format but still, nothing happened. thanx in advance!

                  T Offline
                  T Offline
                  TwoFaced
                  wrote on last edited by
                  #8

                  Try replacing dtgCustomSched.Item(row,2) with a literal string that represents a time. Something like "12:29:01". That should certainly work. Once you've verified that try using a string variable to hold the date instead of using dtgCustomSched.Item(row,2) directly. So simply assign that value to a string and then in the ParseExact line use the string variable instead. Worth a shot I guess. If that doesn't work then go back and double and triple check the string your getting from the DB. Are you sure there aren't any other characters in it that you don't want. A space? Some sort of linefeed character or something silly like that? Basically if the string isn't 8 characters long then something isn't right. In fact try using the string.length function to verify it's length. If you still can't get it I'd try another method. Do as christian said and use the string.split function to parse the numbers out of the string. Once you've got the individual hours, minutes, and seconds you should be able to pass those to the constructor of the date class to create your date object.

                  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