|
-
Jan 14th, 2004, 12:30 PM
#1
Thread Starter
Hyperactive Member
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:
Dim MyDate As Date
Dim MyDay As Integer
MyDate = _
#2/12/1969# ' Assign a date using standard short format.
MyDay = _
Microsoft.VisualBasic.DateAndTime.Day(MyDate) ' MyDay contains 12.
-
Jan 14th, 2004, 03:16 PM
#2
Frenzied Member
Something like this?
VB Code:
Dim myDate As New Date(Now.Year, Now.Month, TextBox1.Text)
' Interesting that these two lines show different results
MsgBox(myDate)
Debug.WriteLine(myDate)
' I think this is what you need. Check out "Standard
' DateTime Format Strings" in MSDN
Debug.WriteLine(myDate.ToString("d"))
HTH,
Mike
-
Jan 14th, 2004, 04:19 PM
#3
Thread Starter
Hyperactive Member
Thanks Mike. All I needed was the very first bit of code you gave me.
VB Code:
If TextBox1.Text < 1 Or TextBox1.Text > 31 Then
MsgBox("Day number not reconized", MsgBoxStyle.OKOnly, "Date Error")
TextBox1.Clear()
TextBox1.Focus()
Else
Dim mydate As New Date(Now.Year, Now.Month, TextBox1.Text)
MsgBox(mydate)
End If
-
Jan 14th, 2004, 04:30 PM
#4
Frenzied Member
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".
-
Jan 14th, 2004, 04:56 PM
#5
Thread Starter
Hyperactive Member
That is not a bad idea especialy when I just realized I have to catch entries that aren'rt numeric.
VB Code:
Try
Dim mydate As New Date(Now.Year, Now.Month, txtDayCode.Text)
txtPostDate.Text = mydate
Catch ex As Exception
MsgBox("Error: " & ex.Message)
txtDayCode.Clear()
txtDayCode.Focus()
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|