Search notes:

VBA: Convert a UTC text to a date

A combination of dateSerial(), timeSerial() and mid() allows to create a date from a string.
The following example tries to demonstrate how a string that is formatted according to UTC (without timezone) might be converted to a date.
The following function UTCStringToDate() tries to demonstrate how these two functions might be combined with mid() to create a date from a UTC string.
option explicit

sub main() ' {

    dim dt_

    dt_ = UTCStringToDate("2018-02-27T15:16:17Z")

    debug.print(format$(dt_, "dd.mm.yyyy - hh:nn:ss"))

end sub ' }

function UTCStringToDate(utc as string) as date ' {

    UTCStringToDate = dateSerial(           _
                         mid(utc, 1, 4),    _
                         mid(utc, 6, 2),    _
                         mid(utc, 9, 2)) +  _
                      timeSerial( _
                         mid(utc,12, 2),    _
                         mid(utc,15, 2),    _
                         mid(utc,18, 2))


end function ' }
Github repository about-VBA, path: /language/datatypes/date/utc-to-date.bas

See also

Date and time related functions

Index