Pythonic way to get datetime from string without leading zero - python

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)

Related

Convert UTC date string to PST and then subtract 12 hours from it

I have incoming strings that are UTC dates in ISO format like,
2021-11-21T12:16:42Z
I wish to convert these dates into PST and then subtract 12 hours from it.
import datetime
time_list_utc=2021-11-21T12:16:42Z
time_list_pst=time_list_utc.convert(pst)
print(time_list_pst-8hours)
I am new to datetime manipulation so any input is greatly appreciated
Use datetime, tzinfo and timedelta modules since Python 3.9:
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
s = '2021-11-21T12:16:42Z'
# Convert to datetime (UTC)
dt_utc = datetime.strptime(utc, "%Y-%m-%dT%H:%M:%S%z")
# Convert to PST
dt_pst = dt_utc.astimezone(ZoneInfo('US/Pacific'))
# Subtract 12 hours
dt = dt_pst - timedelta(hours=12)
Output:
>>> dt
datetime.datetime(2021, 11, 20, 16, 16, 42, tzinfo=zoneinfo.ZoneInfo(key='US/Pacific'))
>>> dt_pst
datetime.datetime(2021, 11, 21, 4, 16, 42, tzinfo=zoneinfo.ZoneInfo(key='US/Pacific'))
>>> dt_utc
datetime.datetime(2021, 11, 21, 12, 16, 42, tzinfo=datetime.timezone.utc)

Converting time_struct with timezone to datetime GMT in Python

I'm retrieving struct_time dates from Feedparser.
Like you can see below, FeedParser automatically parses the dates into GMT (second line, EDT, get converted from 19:19 to 23:19)
FeedParser converted Sun, 06 Sep 2020 23:07:16 GMT into struct_time((2020, 9, 6, 23, 7, 16, 6, 250, 0))
FeedParser converted Fri, 11 Sep 2020 19:19:01 EDT into struct_time((2020, 9, 11, 23, 19, 1, 4, 255, 0))
When I try to convert the struct_time to a datetime with the below, I get GMT + 1 (my timezone).
from datetime import datetime
from time import mktime
datetime.fromtimestamp(mktime(struct_time((2020, 9, 6, 23, 7, 16, 6, 250, 0))))
> datetime.datetime(2020, 9, 7, 0, 7, 16)
How can I convert these struct_time into datetimes GMT?
to get a time zone aware datetime object, you could unpack the relevant part of the timetuple into a datetime object and set the tzinfo attribute to UTC:
from datetime import datetime, timezone
# given a time_struct tuple like
s = (2020, 9, 6, 23, 7, 16, 6, 250, 0)
# convert to datetime object as
dt = datetime(*s[:6], tzinfo=timezone.utc)
print(dt)
>>> 2020-09-06 23:07:16+00:00

Convert Dates in python to compare

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)

Representing date format for three letter month in Python

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)

change datetime object to string

I want to convert datetime.datetime(2016, 11, 21, 5, 34, 38, 826339, tzinfo=<UTC>) as Nov. 21, 2016, 11:04 a.m.
The time in the datetime object is in UST but I want it to be converted into IST(UST+ 05:30).
I tried using strftime as:
>>> datetime(2016, 11, 21, 5, 34, 38, 826339, tzinfo=<UTC>).isoformat(' ')
File "<stdin>", line 1
datetime(2016, 11, 21, 5, 34, 38, 826339, tzinfo=<UTC>).isoformat(' ')
^
SyntaxError: invalid syntax
Can I get some help here.
PS: I am using python
EDIT:
cr_date = datetime(2016, 11, 21, 5, 34, 38, 826339) #excluding the timezone
I can get partial desired reults by:
cr_date.strftime('%b. %d, %Y %H:%M')
'Oct. 31, 2013 18:23
didn't get the am/pm though
For the am/pm part, couldn't you use %p for the last field? I don't know python, I'm just assuming python is taking syntax from the unix date command.
Please find below code
from datetime import datetime
cr_date = datetime(2016, 11, 21, 5, 34, 38, 826339)
cr_date.strftime('%b. %d, %Y %H:%M %P')
'Nov. 21, 2016 05:34 am'
add %p for 'AM or PM' else add %P for 'am or pm'

Categories