DateTime from string

Looks like datetimefrom string can not handle Wed, 12 Oct 2022 21:29:52 +0000

What’s that best way to handle this. Do I really need to make a procedure to do so? I can make a month Array and a Day array but I would hope there should be a way of handling multiple standard formats to be parsed into a date variable.

1 Like

I can’t find a locale for Xojo to use exactly “Wed, 12 Oct 2022 21:29:52 +0000”

You can use datetime from string with “Wed, 12 Oct 2022 at 21:29:52” and create the TimeZone using the +0000, if your string is always the same characters long you may easily create a new string that can be used.

Or maybe you can find a locale that doesn’t use the ‘at hh:mm:ss’ so you will only need to convert the +0000 to GMT or seconds from GMT.

The date is coming from emails so it’s not a locale specific date.

The date format is called RFC 2822 or Internet Message Format. :hugs:

2 Likes

Thank you Beatrix and Sascha.

Timothy, from what I can tell there is no way to convert that string to datetime directly with Xojo. Maybe MBS offers a plugin for that or you will need to convert it.

You can open a Feature Request for Xojo to add the option to do FromString and ToString with that format.

ToString can already take the format you want

Sadly fromString doesn’t have this feature

1 Like

I will need to look on how to use ToString to output “Wed, 12 Oct 2022 21:29:52 +0000”, I don’t see it in the docs.

Edit: didn’t know that you can use format pattern for datetime.ToString, Xojo should add that to FromString.

dateVar.toString(“ccc, dd MMM yyyy HH:mm:ss xx”, new Locale(“en-US”))

1 Like

Thank you Antonio, I created a couple of cases:
Need documentation and examples
#71695 - DateTime ToString documentation, I can’t find information about using a format pattern
If ToString can use the format patterns, FromString should be able to use them too
#71696 - DateTime FromString should allow format pattern as used in ToString

Seems good for most cases:

Public Function FromIMFTime(IMFTime As String) As DateTime

  Const months As String = "Jan?Feb?Mar?Apr?May?Jun?Jul?Aug?Sep?Oct?Nov?Dec?"
  Const matchIt = "[a-zA-Z]{3}\s*,\s+(\d\d|\d)\s+([a-zA-Z]{3})\s+(\d{1,4})\s+(\d\d:\d\d:\d\d)\s+\+?(\-?\d\d\d\d)\s*"
  Var rex As new RegEx
  rex.SearchPattern = matchIt
  Var match As RegExMatch = rex.Search(IMFTime ) // day1 mon2 year3 hh:mm:ss4 zone5
  If match = Nil Then Return Nil // Invalid
  Var day As Integer = match.SubExpressionString(1).Val
  Var month As Integer = months.IndexOf(match.SubExpressionString(2))
  If month < 0 Then Return Nil
  month = month\4+1
  Var year As Integer = match.SubExpressionString(3).Val
  Var hms() As String = match.SubExpressionString(4).Split(":")
  Var n As Integer = match.SubExpressionString(5).Val
  Var zone As Integer = n\100
  zone = (zone * 60 + n - zone * 100) * 60 // in seconds
  Return New DateTime(year, month, day, hms(0).Val, hms(1).Val, hms(2).Val, 0, New TimeZone(zone))
  
End Function


----------- Example:

Var testDate As DateTime = FromIMFTime(" Wed, 12 Oct 2022 21:29:52 -0300 ")

break


1 Like