DateTime Python - 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)

Related

strftime formatting - what am I doing wrong?

I've got a string 3:01 AM - 18 Dec 2017
I've written the following pattern strftime('%-I:%M %p - %-d %b %Y') and I can't seem to get it to work, following this
My notes:
%-I hour 12-hour clock as a decimal (no zero padding)
: separation between hour and minute
%M minute as a zero padded decimal
%p AM/PM
- space-dash-space pattern betwen time and date
%-d date of the month as a decimal (no zero padding)
%b Month abbreviated
%Y Year with century as decimal
df['tweet_date'] = pd.to_datetime(df['tweet_date'], errors='coerce').apply(lambda x: x.strftime('%I:%M %p - %d %b %Y')if not pd.isnull(x) else '')
On another dataframe with a similar column this works:
df2['created_at'] = pd.to_datetime(df2['created_at'], errors='coerce').apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S')if not pd.isnull(x) else '')
df2['created_at'] = df2['created_at'].astype('datetime64[s]')`
where values before formatting look like this for example 2017-10-03T15:48:10.000Z
Your format is fine but some os's can't use the negative formatting for zero-padded units. datetime should be able to parse both padded and non-padded instances of those just fine:
from datetime import datetime as dt
z_time = '06:48 PM - 03 Jun 2021'
nz_time = '6:48 PM - 3 Jun 2021'
dt.strptime(z_time, '%I:%M %p - %d %b %Y')
[out:] datetime.datetime(2021, 6, 3, 18, 48)
dt.strptime(nz_time, '%I:%M %p - %d %b %Y')
[out:] datetime.datetime(2021, 6, 3, 18, 48)
And since you're getting strings from datetimes, you should look whether your os supports certain formatting rules. Here's one for windows.
from datetime import datetime
str="3:01 AM - 18 Dec 2017"
date=datetime.strptime(str,"%I:%M %p - %d %b %Y")
To turn your string into a time, do this:
>>> import time
>>> s = "3:01 AM - 18 Dec 2017"
>>> time.strptime(s,'%I:%M %p - %d %b %Y')
time.struct_time(tm_year=2017, tm_mon=12, tm_mday=18, tm_hour=3, tm_min=1,
tm_sec=0, tm_wday=0, tm_yday=352, tm_isdst=-1)
No hyphens after %. The are not mentioned in the official Python documentation.
I wanted to figure out how to use strftime in a lambda function or better, using the .dt accessor after the column in my dataframe had been converted to a datetime
I couldn't figure this out so I went for the next fastest method
from datetime import datetime
formatted_dates = []
for item in df.tweet_date:
formatted_dates.append(datetime.strptime(item,"%I:%M %p - %d %b %Y"))
df.tweet_date = formatted_dates

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.

how to convert sqlite3 datetime to 24 hrs

I am parsing through a JSON file with a string of datetime like this:
"col_datetime": "10/18/2017 2:45:00 PM"
I am using python's datetime.strptime to make this a sqlite datetime:
datetime.strptime(col_datetime, '%m/%d/%Y %H:%M:%S %p')
this is stored in the sqlite table like this:
2017-10-18 02:45:00.000000
But when I try to parse this using strftime in sqlite3 it seems not getting the PM
strftime('%H:%M',col_datetime)
This returns to:
2:45
Instead of:
14:45
Any thoughts on this?
You need to use %I for 12 hour format in strptime instead of %H.
Here's an example where you can see it works as expected with that change:
>>> datetime.strptime('10/18/2017 2:45:00 PM', '%m/%d/%Y %H:%M:%S %p')
datetime.datetime(2017, 10, 18, 2, 45)
>>> datetime.strptime('10/18/2017 2:45:00 PM', '%m/%d/%Y %I:%M:%S %p')
datetime.datetime(2017, 10, 18, 14, 45)

Parsing string into datetime in Python

I have a date with this format
October 14, 2014 1:35PM PDT
I have this in my python script
import time
u_date = 'October 14, 2014 1:35PM PDT'
print time.strptime(u_date,"%b %d, %y %I:%M%p %Z")
I got this error as a result
ValueError: time data u'October 14, 2014 1:35PM PDT' does not match format '%b %d, %y %I:%M%p %Z'
Can anyone explain to me why is this happening? I'm new to python and any help will be appreciated.
Your format is incorrect; %b takes an abbreviated month, but you have a full month, requiring %B, and you have a full 4-digit year, so use %Y, not %y.
The time library cannot parse timezones, however, you'll have to drop the %Z part here and remove the last characters for this to work at all:
>>> time.strptime(u_date[:-4], "%B %d, %Y %I:%M%p")
time.struct_time(tm_year=2014, tm_mon=10, tm_mday=14, tm_hour=13, tm_min=35, tm_sec=0, tm_wday=1, tm_yday=287, tm_isdst=-1)
You could use the dateutil library instead to parse the full string, it'll produce a datetime.datetime object rather than a time struct:
>>> from dateutil import parser
>>> parser.parse(u_date)
datetime.datetime(2014, 10, 14, 13, 35)

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