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)
Related
I have a date time string something like this
2022-03-21 16:29:01.8593
but I want it to be like
03/21/2022 02:16 PM
Use datetime module:
from datetime import datetime
d = datetime.strptime('2022-03-21 16:29:01.8593', '%Y-%m-%d %H:%M:%S.%f')
s = d.strftime('%m/%d/%Y %I:%M %p')
Output:
>>> d
datetime.datetime(2022, 3, 21, 16, 29, 1, 859300)
>>> s
'03/21/2022 04:29 PM'
Use this link as documentation.
I want to covert a string item to the datetime format that I require.
The string item is this : '3/27/2013 2:54:00 PM'
My code is as follows: map(datetime.datetime.strptime,'3/27/2013 2:54:00 PM', '%m/%d/%Y %I:%M:%S %p')
I get the following error : stray % in format '%'
Any help is appreciated.
Not sure why you use map, you can just use strptime on that string, worked for me
import datetime
dt = datetime.datetime.strptime('3/27/2013 2:54:00 PM', '%m/%d/%Y %I:%M:%S %p')
print(dt)
Is there a way to guess datetime format of a given string in python?
e.g. desired usage would be:
> guess_format('2020-02-24 07:22')
'%Y-%m-%d %H:%M'
There's dateutil project which automates datetime string conversion to valid Datetime objects:
> from dateutil.parser import parse
> parse('2020-02-24 07:22')
datetime.datetime(2020, 2, 24, 7, 22)
but can it produce valid formatting strings?
The pydateinfer package provides the possibility to infer the datetime format string of a given date string.
Example:
>>> import dateinfer
>>> dateinfer.infer(['Mon Jan 13 09:52:52 MST 2014', 'Tue Jan 21 15:30:00 EST 2014'])
'%a %b %d %H:%M:%S %Z %Y'
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 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)