iOS Question is there b4i python function :time.strftime("%Y %m %d",dt.timetuple())

John Woodsmall

Active Member
Licensed User
Longtime User
Python has a very flexible date parser,

--------------------------------
B4X:
from dateutil.parser import parse

import time

def check(s):

  print s + "..."

  try:

    dt = parse(s)

    print "  " +time.strftime("%Y %m %d",dt.timetuple())

  except Exception as x:

    print "  can't parse that: " + repr(x)

check("June 1954")

check("4/5/2012")

check("April 3rd, 1999")

check("1963, may 17th")

check("April 3rd, 1999")

check("April third, 1999")

--------------------------------

June 1954...

  1954 06 07

4/5/2012...

  2012 04 05

April 3rd, 1999...

  1999 04 03

1963, may 17th...

  1963 05 17

April 3rd, 1999...

  1999 04 03

April third, 1999...

 can't parse that: ValueError(u'Unknown string format',)
 

John Woodsmall

Active Member
Licensed User
Longtime User
Ok, thanks for this.

Are there any examples?

(what I am doing is using siri for date input with as many formats as possible).
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   Log(ParseDate("4/5/2012"))
   Log(ParseDate("April 3, 1999"))
   Log(ParseDate("1999, April 23"))
End Sub

Sub ParseDate(s As String) As Long
   For Each f As String In Array("MM/dd/yyyy", "MMM dd, yyyy", "yyyy, MMM dd")
     DateTime.DateFormat = f
     Try
       Dim t As Long = DateTime.DateParse(s)
       Return t
     Catch
     End Try 'ignore
   Next
   Return 0
End Sub
You can add more patterns. They are described here: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

There is no built-in pattern that handles those suffixes (1st or 3rd). You will need to clean them yourself in the code.

Note that you might see exceptions in the logs in debug mode. You can ignore them as they are caught.
 
Upvote 0

John Woodsmall

Active Member
Licensed User
Longtime User
Great works!
by the way does this work with other languages as well?
for the April 3, 1999 in Japanese or other?
 
Upvote 0
Top