0

I'm trying below very simple VBA code to convert to Datetime

Sub datetimedifffference()
Dim d As String
Dim sd As Date
d = "2021-04-06T12:56:16+0000"

sd = Format(CDate(d), "mm-dd-yyyy hh:mm:ss")
Debug.Print sd
End Sub

But it is giving type mismatch error Any help would be highly appreciated

I again tried below, but the difference of time is decreasing instead increasing

Sub datetimedifffference()
Dim d As String
Dim sd As Long
d = "2021-04-06T12:56:16+0000"


sd = DateDiff("n", Now, Format(convertStringtoDate(d))

Debug.Print sd
End Sub


Function convertStringtoDate(stringdate As String) As String
    Dim strings() As String
    strings = Split(stringdate, "T")
 
    convertStringtoDate = strings(0) & " " & Left(strings(1), 8)
    'Debug.Print convertStringtoDate
End Function
ZygD
  • 22,092
  • 39
  • 79
  • 102

3 Answers3

1

T and +0000 should be replaced. Also note that a date is a number and a formatted date is a String.

Sub datetimedifffference()
    Dim d As String
    Dim sd As Date
    
    d = "2021-04-06T12:56:16+0000"
    d = Replace(d, "T", " ")
    d = Replace(d, "+0000", "")

    sd = CDate(d)
    Debug.Print sd
End Sub
BigBen
  • 46,229
  • 7
  • 24
  • 40
0

Looking at your edit, I think you need to change the order of both dates:

Sub datetimedifffference()
  Dim d As String
  Dim sd As Long
  d = "2021-04-06T12:56:16+0000"
  sd = DateDiff("n", convertStringtoDate(d), Now)
  Debug.Print sd
End Sub

Function convertStringtoDate(stringdate As String) As String
  Dim strings() As String
  strings = Split(stringdate, "T")
  convertStringtoDate = CDate(strings(0) & " " & Left(strings(1), 8))
End Function
ZygD
  • 22,092
  • 39
  • 79
  • 102
0

You can use this generic function:

' Converts an ISO 8601 formatted date/time string to a date value.
'
' A timezone info is ignored.
' Optionally, a millisecond part can be ignored.
'
' Examples:
'   2029-02-17T19:43:08 +01.00  -> 2029-02-17 19:43:08
'   2029-02-17T19:43:08         -> 2029-02-17 19:43:08
'   ' IgnoreMilliseconds = False
'   2029-02-17T19:43:08.566     -> 2029-02-17 19:43:08.566
'   ' IgnoreMilliseconds = True
'   2029-02-17T19:43:08.566     -> 2029-02-17 19:43:08.000
'
' 2017-05-24. Gustav Brock. Cactus Data ApS, CPH.
'
Public Function CDateIso8601( _
    ByVal Expression As String, _
    Optional ByVal IgnoreMilliseconds As Boolean) _
    As Date

    Const Iso8601Separator  As String = "T"
    Const NeutralSeparator  As String = " "

    ' Length of ISO 8601 date/time string like: 2029-02-17T19:43:08 [+00.00]
    Const Iso8601Length     As Integer = 19
    ' Length of ISO 8601 date/time string like: 2029-02-17T19:43:08.566
    Const Iso8601MsecLength As Integer = 23
    
    Dim Value       As String
    Dim Result      As Date
    
    Value = Replace(Expression, Iso8601Separator, NeutralSeparator)
    If InStr(Expression, MillisecondSeparator) <> Iso8601Length + 1 Then
        IgnoreMilliseconds = True
    End If
    
    If IgnoreMilliseconds = False Then
        Result = CDateMsec(Left(Value, Iso8601MsecLength))
    Else
        Result = CDate(Left(Value, Iso8601Length))
    End If
    
    CDateIso8601 = Result
    
End Function

Then apply the format of yours to the returned value.

Gustav
  • 53,498
  • 7
  • 29
  • 55