1

I got this JSON in VB6 (not .NET) inside a textbox:

[{"id":123,"key":"h73df", "birth_date":"20180101"}]

It returns no error when posting to an url..but the date is not inserted and I don't know why.

I already try different formats like:

"2018.01.01"
["20180101"]
2018.01.01

but won't work. I think I have to use something like cdate() but then I put all the JSON string into a textbox and all became a simple string..and doesn't work.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
Idle Man
  • 88
  • 9
  • 2
    JSON doesn't really have a date type. People end up throwing in any fanciful concoction they want. You need to find out what the server you are posting to expects and then comply. – Bob77 May 21 '18 at 23:32
  • 1
    Possible duplicate of [The "right" JSON date format](https://stackoverflow.com/questions/10286204/the-right-json-date-format) –  May 22 '18 at 05:14
  • 3
    You need to post the documentation from the site you are trying to post to. It doesn't matter what any of us think is the right date format, all that matters is what that sire or API is expecting. – tcarvin May 22 '18 at 13:22

1 Answers1

1

JSON itself doesn't care what format you use for the date. However, it is most commonly used in the JavaScript format which is ISO 8601 format.

That being said, you can create a string in this format using the following VB6 code (The below returns the time in UTC, if you want to use the local time you will need to call GetLocalTime API instead of GetSystemTime):

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Put inside module:
Private Type SYSTEMTIME '16 Bytes
  wYear         As Integer
  wMonth        As Integer
  wDayOfWeek    As Integer
  wDay          As Integer
  wHour         As Integer
  wMinute       As Integer
  wSecond       As Integer
  wMilliseconds As Integer
End Type

Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Function GetJsonDateTime() As String    
    Dim NowTime As SYSTEMTIME
    Dim sYear, sMonth, sDayOfWeek, sDay, sHour, sMinute, sSecond, sMilliseconds As String
    Dim JsonDateTime As String

    GetSystemTime NowTime

    sYear = Format(NowTime.wYear, "0000")
    sMonth = Format(NowTime.wMonth, "00")
    sDay = Format(NowTime.wDay, "00")
    sHour = Format(NowTime.wHour, "00")          'wHour - or + X depends local timezone
    sMinute = Format(NowTime.wMinute, "00")
    sSecond = Format(NowTime.wSecond, "00")
    sMilliseconds = Format(NowTime.wMilliseconds, "000")

    JsonDateTime = sYear & "-" & sMonth & "-" & sDay & "T" & sHour & ":" & sMinute & ":" & sSecond & "." & sMilliseconds & "Z"

    GetJsonDateTime = JsonDateTime
End Function
vbguyny
  • 1,170
  • 1
  • 10
  • 29