Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date problem

Status
Not open for further replies.

vintl

Technical User
Jun 9, 2000
72
0
0
MY
i got this date where user can enter a date in a text box with a char(10) format. so there is no checking on user entered date. lets say the date format user entered is 06112001, how can i set the logic to verify to make it mm/dd/yyyy format? how can i know whether 06 is month or day and same with 11 is month or day? if i do a check say dd>0 && dd<32 and mm>0 && mm<13 how it knows its month or day if its 06?
 
Hi,

as you said - you cannot know, if 0106 is Jan 6 or Jun 1. Leave it to the user...
Try this:
[tt]
Dim date1, date2, my_date
Dim sInput As String 'your input

date1 = String2Date(sInput, &quot;MMDDYYYY&quot;)
date2 = String2Date(sInput, &quot;DDMMYYYY&quot;)

If date1 Is Null And date2 Is Null Then
'action on wrong input
ElseIf date1 Is Not Null And date2 Is Not Null Then
'action on ambigous date
Else
If date1 Is Null Then
my_date = date2
Else
my_date = date1
End If
End If

[/tt]
The 'String2Date' function can be found under &quot;Sample Functions to Parse Numbers and Strings into Dates&quot; (KB: Visual Basic).
You should modify it, so that it returns null if an error occurs.
If you use other setting than american (MM/DD/YYYY) you should modify it as well...
 
Hi guys,

another very usefull function is isDate() that returns a boolean and checks if the inserted is a date and is a correct one.
examples:
30/02/1978 is not;
/1/2 is not;
12/12/1900 is


Good work,
===================
* Marta Oliveira *
===================
marta100@aeiou.pt
-------------------
CPC_TA- Braga
-------------------
Portugal
===================
 
30/02/1978 IS a date - in Europe. They put the day before the month. If you choose this route, you'll need to be aware of this. Nate Gagne
nathan.gagne@verizon.net
AKA Nick Burns - Your Company's Computer Guy
&quot;Would you like me to save your game of Minesweeper first?&quot;

Like my post? Let me know it was helpful!
 
Hi,

As ija said, the string2Date function is good for conversion, I've modified it to check for valid (European) dates. The returnMonth function (not shown) used in this function is just a case statement used to convert a month from a number to a string (in my case I use a short version, e.g. Jan).

If the user inputs 01032002, the function returns 1 Jan 2002

Function String2Date(s, Fmt As String)
'
' Converts strings to dates depending on Fmt
'
On Error GoTo err_handling

'*** Declare variables
Dim dayEnt As Long, monthEnt As Long, yearEnt As Long
Dim myDate As Date
Dim tempDateTest As String, userDate As String, myDateTest As String

'*** Convert month into form mmm
tempDateTest = returnMonth(Mid(s, 3, 2))

dayEnt = Left(s, 2)
monthEnt = Mid(s, 3, 2)
yearEnt = Mid(s, 5)

myDate = DateSerial(yearEnt, monthEnt, dayEnt)
myDateTest = myDate
userDate = Left(s, 2) + &quot;/&quot; + Mid(s, 3, 2) + &quot;/&quot; + Mid(s, 5)
If Not userDate = myDateTest Then
MsgBox &quot;Date is invalid, please try again&quot;
Exit Function
End If

'*** The original code uses a case statement to allow you to choose which format
'*** of date to use. Although this has been set for one type only
Select Case Fmt
Case &quot;DDMMMYYYY&quot; '27051993
String2Date = Left(s, 2) + &quot; &quot; + tempDateTest + &quot; &quot; & Mid(s, 5)
Case Else
String2Date = Null
End Select

'*** If the first character is a zero, drop it
Dim firstChar As String, length As Long
firstChar = Left(String2Date, 1)
If firstChar = &quot;0&quot; Then
length = Len(String2Date)
String2Date = Right(String2Date, (length - 1))
End If

err_handling:
If Err.Number = 13 Then
Exit Function
End If

End Function


Hope this helps,
pmrankine
 
Just a final note on this. VB decides whether day is before month or vice versa by looking up the setting in Control Panel \ Regional Settings...

If my post was helpful? Let me know!

Click Here
| | | |
V V V V
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top