convert text to date

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i'm sure the solution is easy, but i'm just not getting it

i have a text field ServiceDateTime - 10/01/06-2230

i need to express this as a date mm/dd/yyyy hh:hh
 
Unfortunately, your dash and lack of colon in the time make it a bit
difficult, but here goes:

CDate(Left$([ServiceDateTime] ,InStr(1,[ServiceDateTime],"-")-1) & " " &
Mid$([ServiceDateTime],InStr(1,[ServiceDateTime],"-")+1,2) & ":" &
Right$([ServiceDateTime],2))

or you could learn regular expressions. :-)

Barry
 
that did it. thanks so much.

Barry Gilbert said:
Unfortunately, your dash and lack of colon in the time make it a bit
difficult, but here goes:

CDate(Left$([ServiceDateTime] ,InStr(1,[ServiceDateTime],"-")-1) & " " &
Mid$([ServiceDateTime],InStr(1,[ServiceDateTime],"-")+1,2) & ":" &
Right$([ServiceDateTime],2))

or you could learn regular expressions. :-)

Barry

samuel said:
i'm sure the solution is easy, but i'm just not getting it

i have a text field ServiceDateTime - 10/01/06-2230

i need to express this as a date mm/dd/yyyy hh:hh
 
Barry said:
or you could learn regular expressions. :-)

Alternatively, add the function below to a module, then you can get
the date by passing the format as well as the value you want to
convert to a date:

StringToDate([ServiceDateTime], 'mm/dd/yy-hhnn')


Public Function StringToDate(ByVal sInput As String, sFormat As
String) As Date
Dim sYear As String
Dim sMonth As String
Dim sDay As String
Dim sHour As String
Dim sMinute As String
Dim sSecond As String
Dim sDate As String
Dim sTime As String
Dim iPos As Integer

For iPos = 1 To Len(sInput)
Select Case UCase(Mid(sFormat, iPos, 1))
Case "Y"
sYear = sYear & Mid(sInput, iPos, 1)
Case "M"
sMonth = sMonth & Mid(sInput, iPos, 1)
Case "D"
sDay = sDay & Mid(sInput, iPos, 1)
Case "H"
sHour = sHour & Mid(sInput, iPos, 1)
Case "N"
sMinute = sMinute & Mid(sInput, iPos, 1)
Case "S"
sSecond = sSecond & Mid(sInput, iPos, 1)
Case Else
End Select
Next

sDate = sYear & "/" & sMonth & "/" & sDay
sTime = sHour & ":" & sMinute & IIf(sSecond <> "", ":", "") &
sSecond

StringToDate = 0

If IsDate(sDate) Then
StringToDate = CDate(sDate)
End If

If IsDate(sTime) Then
StringToDate = StringToDate + CDate(sTime)
End If
End Function
 
how would i convert 2005-01-02 14:39:00.000 ?


Barry Gilbert said:
Unfortunately, your dash and lack of colon in the time make it a bit
difficult, but here goes:

CDate(Left$([ServiceDateTime] ,InStr(1,[ServiceDateTime],"-")-1) & " " &
Mid$([ServiceDateTime],InStr(1,[ServiceDateTime],"-")+1,2) & ":" &
Right$([ServiceDateTime],2))

or you could learn regular expressions. :-)

Barry

samuel said:
i'm sure the solution is easy, but i'm just not getting it

i have a text field ServiceDateTime - 10/01/06-2230

i need to express this as a date mm/dd/yyyy hh:hh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top