Results 1 to 4 of 4

Thread: CDate for mm/dd/yyy hh:mm:ss

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    87

    CDate for mm/dd/yyy hh:mm:ss

    Need to write a CDate that is correct.

    CDate(mm/dd/yyyy hh:mm:ss)

    Code:
    alarm = CDate(TextBox10.Text & "/" & TextBox7.Text & "/" & TextBox23.Text & " " & TextBox2.Text & ":" & TextBox3.Text & ":" & TextBox14.Text)
    
                    aampm = ComboBox1.Text
                    ahour = alarm.Hour
                    amin = alarm.Minute
                    asec = alarm.Second
                    amo = alarm.Month
                    ady = alarm.Day
                    ayr = alarm.Year

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: CDate for mm/dd/yyy hh:mm:ss

    Don't use CDate. While it can be used to convert a String to a Date, it shouldn't be. It should only be used to cast an Object reference that is already a Date, e.g. from a DataRow:
    vb.net Code:
    1. Dim dob = CDate(row("DateOfBirth"))
    In your case, why not just use a DateTimePicker control instead of all those separate TextBoxes? That will do the validation and you can simply get a Date from the Value property. If you're determined to use TextBoxes then you should be validating the input first and then using the Date constructor, e.g.
    vb.net Code:
    1. Dim year As Integer
    2. '...
    3. Dim seconds As Integer
    4. Dim alarmTime As Date
    5.  
    6. If Integer.TryParse(TextBox10.Text, year) AndAlso
    7. '...
    8.    Integer.TryParse(TextBox14.Text, seconds) AndAlso seconds >= 0 AndAlso seconds < 60 Then
    9.     alarmTime = New Date(year, ..., seconds)
    10.  
    11.     '...
    12. End If
    If you were going to use a String then you should be using Date.TryParse or Date.TryParseExact:
    vb.net Code:
    1. Dim alarmTime As Date
    2.  
    3. If Date.TryParse($"{TextBox10.Text}/{TextBox7.Text}/{TextBox23.Text} {TextBox2.Text}:{TextBox3.Text}:{TextBox14.Text}", alarmTime) Then
    4.     '...
    5. End If
    Also, what's up with those control names? The numbers are all over the place so there's a great chance that you could use the wrong on in the wrong place but you should not be using numbers anyway. DO NOT just accept the default names for controls. ALWAYS provide your own meaningful name, e.g. yearTextBox and secondsTextBox. That makes your code far easier to read and less error-prone.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: CDate for mm/dd/yyy hh:mm:ss

    Try this. Note the format...

    Code:
    "MM/dd/yyyy hh:mm:ss"
    That's 12 hour clock with no AM/PM. To add that...

    Code:
    "MM/dd/yyyy hh:mm:ss tt"
    To use 24 hour clock instead...

    Code:
    "MM/dd/yyyy HH:mm:ss"
    This is for parsing the date from the string...

    Code:
    alarm = Date.ParseExact(TextBox10.Text & "/" & TextBox7.Text & "/" & TextBox23.Text & " " & TextBox2.Text & ":" & TextBox3.Text & ":" & TextBox14.Text, "MM/dd/yyyy hh:mm:ss", Nothing)

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: CDate for mm/dd/yyy hh:mm:ss

    If you have all of the parts in individual text boxes, why build a string and then parse it? The Date data type has an overload that will take the values and avoid having to build a string.

    https://docs.microsoft.com/en-us/dot...datetime.-ctor

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width