Converting string to date not working - python

I just can't seem to get this to work - I'm pretty sure I've for the syntax for strptime() correct but its not working. The output expected is 31 Aug 2015:
str = '31 Aug 2015 at 23:59'
try:
mydate = datetime.strptime(str, '%d %b %Y')
print mydate
except ValueError:
mydate = None
print "error"
I get "error" printed out. What am I missing?
The str variable is read from a file so it could have any data in it. I'm just looking for entries that have a valid date (Day Month Year) present.

You need to take into account the at 23:59 part too:
>>> from datetime import datetime
>>>
>>> s = '31 Aug 2015 at 23:59'
>>> datetime.strptime(s, "%d %b %Y at %H:%M")
datetime.datetime(2015, 8, 31, 23, 59)
Or, alternatively, let dateutil do the job:
>>> from dateutil.parser import parse
>>>
>>> s = '31 Aug 2015 at 23:59'
>>> parse(s)
datetime.datetime(2015, 8, 31, 23, 59)

Related

Convert string (Jun 6, 2019 9:29:29 AM) into datetime (Y-M-D H:M:S) with Python -

I've the following date: Jun 6, 2019 9:29:29 AM in string format with python and I want to convert this string into 2019-06-06 09:29:29. I've tried with strptime but I always get an error in the processing:
from datetime import datetime
date_string = "Jun 6, 2019 9:29:29 AM"
date_object = datetime.strptime(date_string, "%m %d, %Y %H:%M:%S %p")
Any suggestions?
>>> from datetime import datetime
>>> date_string = "Jun 6, 2019 9:29:29 AM"
>>> date_object = datetime.strptime(date_string,"%b %d, %Y %H:%M:%S %p")
>>> print(date_object)
2019-06-06 09:29:29
fecha="May 29, 2019 9:10:07 PM"
fecha = datetime.strptime(fecha,"%b %d, %Y %H:%M:%S %p")
fecha = str(fecha)
print fecha
2019-05-29 09:10:07
the result is incorrect :( , the transformation from PM isnt correct
regards

Split and Combine Date

I am trying to write a python script that will compare dates from two different pages. The format of date in one page is Oct 03 2016 whereas on other page is (10/3/2016). My goal is to compare these two dates. I was able to convert Oct to 10 but don't know how to make it 10/3/2016.
You should really be using the dateutil library for this.
>>> import dateutil.parser
>>> first_date = dateutil.parser.parse('Oct 03 2016')
>>> second_date = dateutil.parser.parse('10/3/2016')
>>> first_date
datetime.datetime(2016, 10, 3, 0, 0)
>>> second_date
datetime.datetime(2016, 10, 3, 0, 0)
>>> first_date == second_date
True
>>>
Use datetime module to convert your string to datetime object and then compare both. For example:
>>> from datetime import datetime
>>> date1 = datetime.strptime('Oct 03 2016', '%b %d %Y')
>>> date2 = datetime.strptime('10/3/2016', '%m/%d/%Y')
>>> date1 == date2
True
Further, you may convert thisdatetime object to your custom format using datetime.strftime() as:
>>> date1.strftime('%d * %B * %Y')
'03 * October * 2016'
List of all the directives usable for formatting the string are available at the strftime link I mentioned above.

datetime comparison with string?

Given a string in the datetime format of strftime('%b %d, %Y %I:%M %p')
eg: 'Apr 17, 2016 02:00 AM'
is there a way to check if this time has already past?
like may be compare
datetime.datetime.now().strftime('%b %d, %Y %I:%M %p')
somehow to the string 'Apr 17, 2016 02:00 AM'
Parse the string into a datetime.datetime instance then compare to datetime.now():
from datetime import datetime
dt = datetime.strptime('Apr 17, 2016 02:00 AM', '%b %d, %Y %I:%M %p')
if dt < datetime.now():
print('{} alread past'.format(dt))
You might like to make that a function:
def is_past(dt_string):
return datetime.strptime(dt_string, '%b %d, %Y %I:%M %p') < datetime.now()
And then call it like this:
>>> is_past('Apr 17, 2016 02:00 AM')
False
>>> is_past('Apr 01, 2016 02:00 AM')
True
No, string comparison would not work here; the values do not sort according to the full date, but according to the month and day of the month, with the first 10 days of the month eratically dispersed throughout that sort as the day number is not zero-padded. If the year was listed first, followed by a zero-padded numeric month, day, hours (using a 24 hour clock) and minutes, then you could do it as strings only, because then the lexicographical sort happens to match the way the date and time would be sorted.
Instead, parse your string to a datetime object using the datetime.datetime.strptime() class method:
dt = datetime.datetime.strptime(string, '%b %d, %Y %I:%M %p')
if dt < datetime.datetime.now():
# in the past.
Demo:
>>> import datetime
>>> string = 'Apr 17, 2016 02:00 AM'
>>> dt = datetime.datetime.strptime(string, '%b %d, %Y %I:%M %p')
>>> dt
datetime.datetime(2016, 4, 17, 2, 0)
>>> dt < datetime.datetime.now()
False
If you were to format your dates using the ISO8601 combined date-time format, then you'd have a format that can be sorted as strings only:
>>> datetime.datetime.now().isoformat()
'2016-04-15T09:55:09.907130'

Sort this list of tuples by the time span

content_dict = [(u'Bowe Bergdahl', [u'Sat, 31 May 2014 16:03:32 EDT']), (u"U.S. 'hypocrisy' in cybertheft charge", [u'Fri, 23 May 2014 02:30:44 EDT'])
What is wrong with this lambda function:
content2_dict = sorted(content_dict, key=lambda x: datetime.strptime(x[0],'%a, %d %b %H:%M:%S %z'))
I get the following error:
Traceback (most recent call last):
File "/Users/amirnakhostin/Documents/Computer Science /Python/test.py", line 17, in <module>
content2_dict = sorted(content_dict, key=lambda x: datetime.strptime(x[1],'%a, %d %b %H:%M:%S'))
File "/Users/amirnakhostin/Documents/Computer Science /Python/test.py", line 17, in <lambda>
content2_dict = sorted(content_dict, key=lambda x: datetime.strptime(x[1],'%a, %d %b %H:%M:%S'))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data 'o' does not match format '%a, %d %b %H:%M:%S'
I also had a problem with a timezone, had to remove it.
import datetime
content_dict = [(u'Bowe Bergdahl', [u'Sat, 31 May 2014 16:03:32']), (u"U.S. 'hypocrisy' in cybertheft charge", [u'Fri, 23 May 2014 02:30:44'])]
content2_dict = sorted(content_dict, key=lambda x: datetime.datetime.strptime(x[1][0],'%a, %d %b %Y %H:%M:%S'))
print content2_dict
Several problems with your code. You improperly formatted strptime by omitting the year %Y. You also enclosed the date in a list which caused other problems. You were trying to use x[0] when your date was enclosed in x[1]. Here is a working example of (almost all) of your code (note that the time zone has been removed. See below for why).
>>> content = [(u'Bowe Bergdahl',u'Sat, 31 May 2014 16:03:32'), (u"U.S. 'hypocrisy' in cybertheft charge", u'Fri, 23 May 2014 02:30:44')]
>>> content2 = sorted(content, key=lambda x:datetime.datetime.strptime(x[1], '%a, %d %B %Y %H:%M:%S'))
>>> content2
[(u"U.S. 'hypocrisy' in cybertheft charge", u'Fri, 23 May 2014 02:30:44'), (u'Bowe Bergdahl', u'Sat, 31 May 2014 16:03:32')]
However we get from this question that strptime has some issues with time zones. To fix that issue we use the dateutil package
>>> from dateutil import parser
>>> parser.parse(u'Sat, 31 May 2014 16:03:32 -0400')
datetime.datetime(2014, 5, 31, 16, 3, 32, tzinfo=tzoffset(None, -14400))
Note that -0400 is EDT relative to GMT.
To use this to sort your list do something like the following
>>> from dateutil import parser
>>> unformatted_content = [(u'Bowe Bergdahl', u'Sat, 31 May 2014 16:03:32 -0400'), (u"U.S. 'hypocrisy' in cybertheft charge", u'Fri, 23 May 2014 02:30:44 -0400')]
>>> real_content = [(item[0], parser.parse(item[1])) for item in unformatted_content]
>>> real_content
[(u'Bowe Bergdahl', datetime.datetime(2014, 5, 31, 16, 3, 32, tzinfo=tzoffset(None, -14400))), (u"U.S. 'hypocrisy' in cybertheft charge", datetime.datetime(2014, 5, 23, 2, 30, 44, tzinfo=tzoffset(None, -14400)))]
>>> content2 = sorted(real_content, key=lambda x:x[1])
>>> content2
[(u"U.S. 'hypocrisy' in cybertheft charge", datetime.datetime(2014, 5, 23, 2, 30, 44, tzinfo=tzoffset(None, -14400))), (u'Bowe Bergdahl', datetime.datetime(2014, 5, 31, 16, 3, 32, tzinfo=tzoffset(None, -14400)))]
There is a missing %Y between %b and %H:%M:%S.
To access a time string in your content_dict, x[1][0] should be used instead of x[0].
Here lies a workaround solution, since python (or datetime module) has some problem handling time zones (HKT can be parsed while EDT can't which confuses me) and assuming that all the timezones are the same, I just simply stripped all the time zones away.
content2_dict = sorted(content_dict, key=lambda x: datetime.strptime(x[1][0][:-4], '%a, %d %b %Y %H:%M:%S'))
There are a couple problems, you are indexing the wrong thing in your lambda, you forgot a year, %H:%M:%S can be written as %X, and EDT, unfortunately, can't be parsed:
from datetime import datetime
content_dict = [(u'Bowe Bergdahl', [u'Sat, 31 May 2014 16:03:32']), (u"U.S. 'hypocrisy' in cybertheft charge", [u'Fri, 23 May 2014 02:30:44'])]
content_dict_ = sorted(content_dict, key=lambda x: datetime.strptime(x[1][0],'%a, %d %B %Y %X'))

How to get datetime object from string "DD Month YYY" where month is in full name ex. January

I've a string, for ex '03 July 2012' and i want a datetime object from this. I'm looking for the most optimum way to do it.
Did you read the documentation?
>>> import datetime
>>> datetime.datetime.strptime('03 July 2012', '%d %B %Y')
datetime.datetime(2012, 7, 3, 0, 0)
%d stays for day (03), %B for full month day (July) and %Y for year (2012).

Categories

Resources