How do I represent a 3 letter month date format in python such as the following:
Jan 16, 2012
I know for January 16, 2012 the format is %B %d,%Y. Any ideas?
There's the three letter month format %b:
In [37]: datetime.strptime('Jan 16, 2012', '%b %d, %Y')
Out[37]: datetime.datetime(2012, 1, 16, 0, 0)
date_str = 'Jan 16, 2012'
date_components = date_str.split(' ')
date_components[0] = date_components[0][:3]
return ' '.join(date_components)
Related
I have an array of dates that is formatted like so:
['October 22nd, 2019', 'February 8th, 2020', 'July 31st, 2020', 'September 21st, 2020', ...]
I'd like to turn it into datetime objects using strptime, but I can't figure out how to hand the spelled out parts of the days, e.g. 22nd or 8th and it doesn't say in the format documentation.
The following works when there's no written out part of the day:
from datetime import datetime
dt_obj = datetime.striptime('October 22, 2019', '&B &d, &Y')
But I can't figure out how to parse a string that has the day written out:
in: dt_obj = datetime.striptime('October 22nd, 2019', '&B &d, &Y')
out: ValueError: time data 'October 22nd, 2019' does not match format '%B %d, %Y'
What's the proper format for this? Thank you!
e.g. 22nd or 8th and it doesn't say in the format
documentation
You got it right, it is not mentioned in documentation because there is no such formats, one way you can parse them is by using regex, and converting those date strings to something for which Python's datetime has the format for.
import re
from datetime import datetime
[datetime.strptime(x, '%B %d %Y') for x in [' '.join(re.findall('^\w+|\d+',each)) for each in ['October 22nd, 2019', 'February 8th, 2020', 'July 31st, 2020', 'September 21st, 2020']]]
#output:
[datetime.datetime(2019, 10, 22, 0, 0), datetime.datetime(2020, 2, 8, 0, 0), datetime.datetime(2020, 7, 31, 0, 0), datetime.datetime(2020, 9, 21, 0, 0)]
I'm working on a python project where I'm getting the following variable from a third party service as an example var = "November 19 2019, 12:00 PM" and I need to convert it to var = "2019-11-19T12:00" in order to send it to google calendar API request in python
since I'm new to python I know there must be a better way but what I got so far to extract the month is :
Try this out:
from datetime import datetime
month_map = {'January': 1, 'February': 2, 'March': 3, 'April': 4, 'May': 5, 'June': 6, 'July': 7, 'August': 8, 'September': 9, 'October': 10, 'November': 11, 'December': 12}
s = "November 19 2019, 12:00 AM"
date_time_list = s.split(' ')
month = month_map[date_time_list[0]]
date = date_time_list[1]
year = date_time_list[2][:4]
hour_min_list = date_time_list[3].split(':')
hour = hour_min_list[0]
if date_time_list[4] == 'PM' and int(hour) != 12:
hour = str(int(hour) + 12)
if date_time_list[4] == 'AM' and int(hour) == 12:
hour = '00'
minutes = hour_min_list[1]
iso_format = datetime(int(year), int(month), int(date), int(hour), int(minutes)).isoformat()
print(iso_format)
A shorter implementation:
from datetime import datetime
s = "November 19 2019, 12:00 AM"
d = datetime.strptime(s, "%B %d %Y, %I:%M %p")
print(d.isoformat())
I have two dates in Python. One is received from the database:
(datetime.datetime(2017, 10, 10, 10, 10, 10),)
And the other from the email:
Thu, 18 Jan 2018 15:50:49 -0500.
I want to compare these two dates in Python for which I think these dates need to be converted in one specific format. How to convert these dates in one specific format to compare?
Convert both to datetime (very powerful with dates).
dt_date = datetime.datetime(2017, 10, 10, 10, 10, 10)
str_date = 'Thu, 18 Jan 2018 15:50:49 -0500'
pd.to_datetime([dt_date,str_date])
Output:
DatetimeIndex(['2017-10-10 10:10:10', '2018-01-18 20:50:49']
from datetime import datetime
start_date = datetime(2017, 10, 10, 10, 10, 10)
email_date = datetime.strptime("Thu, 18 Jan 2018 15:50:49", "%a, %d %b %Y %H:%M:%S")
start_date, email_date
>>>datetime.datetime(2017, 10, 10, 10, 10, 10) datetime.datetime(2018, 1, 18, 15, 50, 49)
Pythonic way to get datetime from a string without leading zeroes?
e.g. no leading zero for Hour (typical case)
'Date: Jul 10, 2014 4:41:28 PM'
dateutil would handle it from out-of-the-box (fuzzy helps to ignore unrelated parts of the string):
>>> from dateutil import parser
>>> s = "Date: Jul 10, 2014 4:41:28 PM"
>>> parser.parse(s, fuzzy=True)
datetime.datetime(2014, 7, 10, 16, 41, 28)
Without dateutil:
>>> import datetime
>>> d = datetime.datetime.strptime(s, 'Date: %b %d, %Y %I:%M:%S %p')
>>> d.hour
16
>>> d
datetime.datetime(2014, 7, 10, 16, 41, 28)
I have the day of the year, or as its called in Python, the tm_yday.
I want to get out the month and day of month.
How can I create a Python time struct from just the day of year?
You can use datetime.strptime to parse the day of the year:
>>> from datetime import datetime
>>> tm_yday = 59
>>> tm_year = datetime.now().year # current year
>>> datetime.strptime('{} {}'.format(tm_year, tm_yday), '%Y %j')
datetime.datetime(2014, 2, 28, 0, 0)
import datetime
>>> datetime.datetime.strptime('10', '%j')
datetime.datetime(1900, 1, 10, 0, 0)
>>> datetime.datetime.strptime('359', '%j')
datetime.datetime(1900, 12, 25, 0, 0)
>>> datetime.datetime.strptime('359', '%j').month
12
>>> datetime.datetime.strptime('359', '%j').day
25
import datetime
foo=datetime.datetime(1990, 1, 1) + datetime.timedelta(tm_yday - 1)
month = foo.month
dayOfMonth = foo.day
Year 1990 is an example, try other in the case of leap year.
Datetime date types are the way to go here:
import datetime
Here's today:
>>> datetime.date.today()
datetime.date(2014, 2, 27)
And here's Jan 1st of 2014:
>>> datetime.date(2014, 1, 1)
datetime.date(2014, 1, 1)
To combine into a datetime object:
datetime.datetime.combine(datetime.date(2014, 1, 1),
datetime.datetime.min.time())