Python datetime Period Ignored by strptime? [duplicate] - 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.

Format Unicode time value

I have next time value in unicode (<type 'unicode'>):
2017-08-09T15:02:58+0000.
How to convert it to friendly view (e.g. Day, Month of Year)?
This should do what you ask:
from datetime import datetime
a = '2017-08-09T15:02:58+0000'
datetime.strptime(a[:-5], '%Y-%m-%dT%H:%M:%S').strftime('%d, %b of %Y')
#09, Aug of 2017
strptime method throws error for timezone parameter that doesn't seem to interest you so I removed that part with a[:-5].
For the rest of the string you can just follow guidelines from datetime docs.
Using the same docs you can construct your datetime string using strftime() method like you wanted '%d, %b of %Y' or in plain words [day], [abbreviated month] of [Year]
try this
import datetime
today = datetime.date.today()
print today.strftime('We are the %d, %b %Y')
'We are the 22, Nov 2008'

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')

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)

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

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.

Categories

Resources