Results 1 to 5 of 5

Thread: Creating a short date based on the day of month

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    Creating a short date based on the day of month

    I know there must be a simple way to do this but it is eluding me so far. I want to create a short date based on a number entered into a text box. If, for instance, a 12 is entered I would like it to set the default date to 1/12/2004 (current month)

    Basicaly I want to do the opposite of this sub that I found in Help
    VB Code:
    1. Dim MyDate As Date
    2.         Dim MyDay As Integer
    3.         MyDate = _
    4.             #2/12/1969# ' Assign a date using standard short format.
    5.         MyDay = _
    6.             Microsoft.VisualBasic.DateAndTime.Day(MyDate) ' MyDay contains 12.

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Something like this?

    VB Code:
    1. Dim myDate As New Date(Now.Year, Now.Month, TextBox1.Text)
    2.  
    3.         ' Interesting that these two lines show different results
    4.         MsgBox(myDate)
    5.         Debug.WriteLine(myDate)
    6.  
    7.         ' I think this is what you need. Check out "Standard
    8.         ' DateTime Format Strings" in MSDN
    9.         Debug.WriteLine(myDate.ToString("d"))

    HTH,
    Mike

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    Thanks Mike. All I needed was the very first bit of code you gave me.
    VB Code:
    1. If TextBox1.Text < 1 Or TextBox1.Text > 31 Then
    2.             MsgBox("Day number not reconized", MsgBoxStyle.OKOnly, "Date Error")
    3.             TextBox1.Clear()
    4.             TextBox1.Focus()
    5.         Else
    6.             Dim mydate As New Date(Now.Year, Now.Month, TextBox1.Text)
    7.             MsgBox(mydate)
    8.         End If

  4. #4
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Cool. As a side note, you may consider just trapping the exception that happens when you try to create a new date.

    That code would throw an unhandled exception if you run it next month, for example, putting in 30 for the day. Then you have to think of leap years.

    Less work catching the exception and saying "Bad Date".

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    That is not a bad idea especialy when I just realized I have to catch entries that aren'rt numeric.
    VB Code:
    1. Try
    2.             Dim mydate As New Date(Now.Year, Now.Month, txtDayCode.Text)
    3.             txtPostDate.Text = mydate
    4.         Catch ex As Exception
    5.             MsgBox("Error: " & ex.Message)
    6.             txtDayCode.Clear()
    7.             txtDayCode.Focus()
    8.         End Try

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