This is probably a very basic question but after reading documentation I still can't figure out how to do it...
I have two strings in Python that contain dates of unknown format. I don't know what formats they are in, except I know that both are valid date-time expressions. For example, one of them might be in the ISO format and the other in some other format.
All I need is to be able to compare the dates. What's the correct way to turn strings into appropriate date-time objects so that they can be compared?
thanks!
We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale. ENGLISH); String dateInString = "7-Jun-2013"; Date date = formatter. parse(dateInString); In the above example, we first need to construct a SimpleDateFormat object by passing the pattern describing the date and time format.
We can convert String to Date in java using parse() method of DateFormat and SimpleDateFormat classes.
Import the datetime library. Use the datetime. datetime class to handle date and time combinations. Use the strptime method to convert a string datetime to a object datetime.
The dateutil module has a date parser which can parse date strings in many formats.
For example,
In [13]: import dateutil.parser as parser In [14]: parser.parse("19970902T090000") Out[14]: datetime.datetime(1997, 9, 2, 9, 0) In [15]: import datetime as dt In [16]: now = dt.datetime.now() In [17]: now.isoformat() Out[18]: '2012-11-06T15:08:51.393631' In [19]: parser.parse('2012-11-06T15:08:51.393631') Out[19]: datetime.datetime(2012, 11, 6, 15, 8, 51, 393631) In [20]: parser.parse('November 6, 2012') Out[20]: datetime.datetime(2012, 11, 6, 0, 0)
Note that some datetime strings can be ambiguous: 10-09-2003
could mean October 9 or September 10, for example. dateutil
has parameters like dayfirst
and yearfirst
to handle this:
In [21]: parser.parse("10-09-2003") Out[21]: datetime.datetime(2003, 10, 9, 0, 0) In [22]: parser.parse("10-09-2003", dayfirst = True) Out[22]: datetime.datetime(2003, 9, 10, 0, 0) In [23]: parser.parse("10-09-03", yearfirst = True) Out[23]: datetime.datetime(2010, 9, 3, 0, 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With