How can I account for period (AM/PM) using strftime? - python

Specifically I have code that simplifies to this:
from datetime import datetime
date_string = '2009-11-29 03:17 PM'
format = '%Y-%m-%d %H:%M %p'
my_date = datetime.strptime(date_string, format)
# This prints '2009-11-29 03:17 AM'
print my_date.strftime(format)
What gives? Does Python just ignore the period specifier when parsing dates or am I doing something stupid?

The Python time.strftime docs say:
When used with the strptime() function, the %p directive only
affects the output hour field if the %I directive is used to parse
the hour.
Sure enough, changing your %H to %I makes it work.

format = '%Y-%m-%d %H:%M %p'
The format is using %H instead of %I. Since %H is the "24-hour" format, it's likely just discarding the %p information. It works just fine if you change the %H to %I.

You used %H (24 hour format) instead of %I (12 hour format).

Try replacing %H (Hour on a 24-hour clock) with %I (Hour on a 12-hour clock) ?

>>> from datetime import datetime
>>> print(datetime.today().strftime("%H:%M %p"))
15:31 AM
Try replacing %I with %H.

Related

Python strptime not parsing time zone EST %z

I am trying to parse the string '10/23/2019 6:02:05 PM EST' into a datetime with time zone using Python 3.7.
Code:
from datetime import datetime
timestamp = datetime.strptime(date_str, '%m/%d/%Y %I:%M:%S %p %Z')
Error:
ValueError: time data '10/23/2019 6:02:05 PM EST' does not match format '%m/%d/%Y %I:%M:%S %p %Z'
When I create a datetime and output it using the same formatting I get the correct output. The only difference is that there is a 0 in front of the hour, but adding 0 in front of the 6 in my date string results in the same error.
My current solution is to parse the datetime without the timezone and then localize it, but this is not ideal.
date_lst = date.split()
date_str = ' '.join(date_lst[0:3])
timestamp = datetime.strptime(date_str, '%m/%d/%Y %I:%M:%S %p')
new_tz = pytz.timezone(date_lst[3])
timestamp_tz = new_tz.localize(timestamp)```
It is reasonable to expect that parsing a string with a timezone included would produce a timezone aware datetime object.
Try it
>>timestamp = datetime.strptime('10/23/2019 6:02:05 PM EST', '%m/%d/%Y %I:%M:%S %p EST')
>>2019-10-23 06:02:05
You can try this.

Convert datetime in python

How do i convert this datetime using python?
2017-10-16T08:27:16+0000
I tried to use strptime but getting ValueError: time data '2017-10-16T08:27:16+0000' does not match format 'The %d %B %Y at. %H:%M'
import datetime
datetime.datetime.strptime("2017-10-16T08:27:16+0000", "The %d %B %Y at. %H:%M")
'''
I want my output to look like this
The 16 october 2017 at. 08:27
'''
First parse the string correctly, then print it in the desired format:
import datetime
date = datetime.datetime.strptime("2017-10-16T08:27:16+0000", "%Y-%m-%dT%H:%M:%S%z")
print(date.strftime("The %d %B %Y at. %H:%M"))
https://blogs.harvard.edu/rprasad/2011/09/21/python-string-to-a-datetime-object/
You have to first strip your date using strptime() and then rebuild it using strftime()
import datetime
time = "2017-10-16T08:27:16+0000"
stripedTime = datetime.datetime.strptime(time, '%Y-%m-%dT%I:%M:%S%z')
rebuildTime = stripedTime.strftime('The %d %B %Y at. %H:%M')
print(rebuildTime)

how do you format date time in python

I have a variable that has value like this:
val='14/12/15 0000'
it is in two digit year/month/day hourminute format.
I need to convert this to epoch time.
I tried this
import datetime
datetime.datetime.strptime(val, "%y/%m/%d %HH%MM").strftime('%s')
I get this error:
ValueError: time data '14/12/15 0000' does not match format '%y/%m/%d %HH%MM'
what am I doing wrong here?
Hours (24 hr) are %H, not %HH, and minutes are %M, not %MM.
datetime.datetime.strptime(val, "%y/%m/%d %H%M").strftime('%s')
You can use easy_date to make it easy:
import date_converter
my_datetime = date_converter.string_to_string('14/12/15 0000', '%y/%m/%d %H%M', '%s')
Or even convert directly to a timestamp:
import date_converter
timestamp = date_converter.string_to_timestamp('14/12/15 0000', '%y/%m/%d %H%M')

Python datetime Period Ignored by strptime? [duplicate]

Specifically I have code that simplifies to this:
from datetime import datetime
date_string = '2009-11-29 03:17 PM'
format = '%Y-%m-%d %H:%M %p'
my_date = datetime.strptime(date_string, format)
# This prints '2009-11-29 03:17 AM'
print my_date.strftime(format)
What gives? Does Python just ignore the period specifier when parsing dates or am I doing something stupid?
The Python time.strftime docs say:
When used with the strptime() function, the %p directive only
affects the output hour field if the %I directive is used to parse
the hour.
Sure enough, changing your %H to %I makes it work.
format = '%Y-%m-%d %H:%M %p'
The format is using %H instead of %I. Since %H is the "24-hour" format, it's likely just discarding the %p information. It works just fine if you change the %H to %I.
You used %H (24 hour format) instead of %I (12 hour format).
Try replacing %H (Hour on a 24-hour clock) with %I (Hour on a 12-hour clock) ?
>>> from datetime import datetime
>>> print(datetime.today().strftime("%H:%M %p"))
15:31 AM
Try replacing %I with %H.

DateTime Python

How to convert "12/17/2010 4:12:12 PM" to a datetime object?
For eg, If it was like "2007-03-04T21:08:12Z", I would have done
dd =datetime.strptime( "2007-03-04T21:08:12Z", "%Y-%m-%dT%H:%M:%SZ" )
but for time with AM/PM is there any direct way of doing?
From the strptime(3) man page:
%I The hour on a 12-hour clock (1-12).
...
%p The locale’s equivalent of AM or PM. (Note: there may be none.)
you can use below to handle "AM" or "PM" in a date
%I refers 12-hour format
%H refers 24-hours format
t = "12/17/2010 4:12:12 PM"
res = datetime.datetime.strptime(t, "%m/%d/%Y %I:%M:%S %p")
print res
datetime.datetime(2010, 12, 17, 16, 12, 12)
%p - refers am/AM/pm/PM.
d1= '12/17/2010 4:12:12 PM'
fmt = '%m/%d/%Y %H:%M:%S %p'
d2=datetime.datetime.strptime(d1, fmt)

Categories

Resources