vba: convert a datetime-string yyyymmddhhmmss to date

Public Function Conv2Datetime(sDatetimestamp As String) As Date
‘Format of sDatetimestamp:= yyyymmddhhmmss

Dim dtm As Date
Dim dS As Date
Dim dT As Date

dS = DateSerial(CInt(Left(sDatetimestamp, 4)), CInt(Mid(sDatetimestamp, 5, 2)), CInt(Mid(sDatetimestamp, 7, 2)))
dT = TimeSerial(CInt(Mid(sDatetimestamp, 9, 2)), CInt(Mid(sDatetimestamp, 11, 2)), CInt(Mid(sDatetimestamp, 13, 2)))
dtm = CDate(Str(dS) + ” ” + Str(dT))

Conv2Datetime = dtm

End Function

Public Function Conv2Date(sDatetimestamp As String) As Date
‘Format of sDatetimestamp:= yyyymmddhhmmss

Dim dS As Date

dS = DateSerial(CInt(Left(sDatetimestamp, 4)), CInt(Mid(sDatetimestamp, 5, 2)), CInt(Mid(sDatetimestamp, 7, 2)))

Conv2Date = dS

End Function

Public Function Conv2Time(sDatetimestamp As String) As Date
‘Format of sDatetimestamp:= yyyymmddhhmmss

Dim dT As Date

dT = TimeSerial(CInt(Mid(sDatetimestamp, 9, 2)), CInt(Mid(sDatetimestamp, 11, 2)), CInt(Mid(sDatetimestamp, 13, 2)))

Conv2Time = dT

End Function

Advertisements

2 responses to “vba: convert a datetime-string yyyymmddhhmmss to date

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s