locked
String to datetime RRS feed

  • Question

  • Hello all!

    I'm having a hard time trying to convert a String to a date. I do this because i need to attach a specific hour to the date time obejct.

    Any ideas? Thanks in advance!

    Here is my code:

     

    Dim Desdex As String = Desde.Text
            Dim Hastax As String = Hasta.Text
            Dim time1 As String = " 00:00:00.000 AM"
            Dim time2 As String = " 12:59:00.000 PM"


            Desdex = Desdex & " " & time1
            Hastax = Hastax & " " & time2
           

            Dim fDesde As DateTime = Convert.ToDateTime(Desdex)
            Dim fhasta As DateTime = Convert.ToDateTime(Hastax)

    Monday, November 15, 2010 9:01 PM

Answers

  • The issue is the time you are trying to give. There is actually no such time as 00:00:00, despite what your experience tells you. The military does not use the time 0000, either. At any rate, potential flamewars aside...here is how I would write that code....

     

    Dim today As DateTime = DateTime.Now

     

    Dim Desdex As String = today.Date

     

    Dim Hastax As String = "10/22/1962"

     

    Dim time1 As String = " 12:00:01 AM"

     

    Dim time2 As String = " 12:59:00.000 PM"

     

    Desdex = Desdex &

    " " & time1

    Hastax = Hastax &

    " " & time2

     

    Dim fDesde As DateTime = Convert.ToDateTime(Desdex)

     

    Dim fhasta As DateTime = Convert.ToDateTime(Hastax)

    MessageBox.Show(

    "fDesde = " & fDesde.ToString & vbCrLf & _

     

    "fhasta = " & fhasta.ToString)

     

    Tuesday, November 16, 2010 5:06 PM
  • Convert what string?  One from the DateTimePicker?  The DateTimePicker has a .Value property which is already a DateTime.  Why can't you use that?

    If you need to take that date and set a specific hour, you can use the constructor of the DateTime class to create a new DateTime using the date from the DateTimePicker and specify specific values for the time:


    'dtp is the name of the DateTimePicker (shortened for this post)
    Dim newDateTime As New DateTime(dtp.Value.Year, dtp.Value.Month, dtp.Value.Day, 23, 59, 0)
    
    
    
    

     

    Tuesday, November 16, 2010 6:36 PM

All replies

  • What values do you have in the text boxes for date?

    --
    Mike
    Monday, November 15, 2010 10:40 PM
  • there's an overload of New DateTime that accepts year, month, day, hour, minute, second
    Monday, November 15, 2010 10:41 PM
  • The datetimepicker comes with todays date. when i try to convert the string to a date, gives me the following msg:

    FormatException was Unhandled

    String was not recognized as a valid DateTime.

     

    Tuesday, November 16, 2010 2:52 PM
  • The issue is the time you are trying to give. There is actually no such time as 00:00:00, despite what your experience tells you. The military does not use the time 0000, either. At any rate, potential flamewars aside...here is how I would write that code....

     

    Dim today As DateTime = DateTime.Now

     

    Dim Desdex As String = today.Date

     

    Dim Hastax As String = "10/22/1962"

     

    Dim time1 As String = " 12:00:01 AM"

     

    Dim time2 As String = " 12:59:00.000 PM"

     

    Desdex = Desdex &

    " " & time1

    Hastax = Hastax &

    " " & time2

     

    Dim fDesde As DateTime = Convert.ToDateTime(Desdex)

     

    Dim fhasta As DateTime = Convert.ToDateTime(Hastax)

    MessageBox.Show(

    "fDesde = " & fDesde.ToString & vbCrLf & _

     

    "fhasta = " & fhasta.ToString)

     

    Tuesday, November 16, 2010 5:06 PM
  • Convert what string?  One from the DateTimePicker?  The DateTimePicker has a .Value property which is already a DateTime.  Why can't you use that?

    If you need to take that date and set a specific hour, you can use the constructor of the DateTime class to create a new DateTime using the date from the DateTimePicker and specify specific values for the time:


    'dtp is the name of the DateTimePicker (shortened for this post)
    Dim newDateTime As New DateTime(dtp.Value.Year, dtp.Value.Month, dtp.Value.Day, 23, 59, 0)
    
    
    
    

     

    Tuesday, November 16, 2010 6:36 PM
  • Hello,

    I'm wondering why You do not use the DateTime.TryParse Method (String, DateTime%)

    http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx

    regards Ellen


    Ich benutze/ I'm using VB2008 & VB2010
    Tuesday, November 16, 2010 8:40 PM