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'
Related
Why is this failing:
datetime.datetime.strptime(
date_string, ' %A %d %B %Y : %I:%M %p')
ValueError("time data ' Tuesday 08 September 2020 : 00:07 AM' does not match format ' %A %d %B %Y : %I:%M %p'")
when this works:
datetime.datetime.strptime(' Wednesday 02 September 2020 : 2:54 AM', ' %A %d %B %Y : %I:%M %p')
I think it must have to do with the 00 hours, but what it is exactly I do not know.
From datetime docs:
%H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, …, 23
%I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, …, 12
So you would need input like 12:07 am for the time to be valid (there is no 0 o'clock in a 12 hour clock)
>> datetime.datetime.strptime(' Tuesday 01 September 2020 : 12:07 AM', ' %A %d %B %Y : %I:%M %p')
datetime.datetime(2020, 9, 1, 0, 7)
Or use %H for 24 hour clock, in which case you'd likely want to drop the %p since it's meaningless in this case
>> datetime.datetime.strptime(' Tuesday 01 September 2020 : 00:07', ' %A %d %B %Y : %H:%M')
datetime.datetime(2020, 9, 1, 0, 7)
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
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)
This question already has answers here:
Convert string date into date format in python?
(3 answers)
Closed 7 years ago.
How to convert 2015 June 1 into date format in python like date_object = datetime.date(2014, 12, 4)
You can use the format - '%Y %B %d' along with datetime.datetime.strptime() method to convert string to date. Where %Y is 4 digit year, %B is complete month name, and %d is date.
Example/Demo -
>>> datetime.datetime.strptime('2015 June 1','%Y %B %d')
datetime.datetime(2015, 6, 1, 0, 0)
>>> datetime.datetime.strptime('2015 June 1','%Y %B %d').date()
datetime.date(2015, 6, 1)
Use the first one, if you are content with datetime object, if you want the date() object itself, you can use the second one.
You can use the date constructor
>>> from datetime import date
>>> date_object = date(year=2015, month=6, day=1)
>>> print date_object
2015-06-01
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).