Parse english textual date expression into datetime [duplicate] - python

This question already has answers here:
Natural language time parser [closed]
(2 answers)
Closed 9 years ago.
How do I convert these strings:
one hour ago
three days ago
two weeks ago
yesterday
next month
into Python datetime object?

Just found parsedatetime for parsing human readable date/time text from the link provided by Jon Clements. Here is a solution in case you interested:
from time import mktime
from datetime import datetime
import parsedatetime as pdt
time_str = '1 hour ago'
cal = pdt.Calendar()
dt = datetime.fromtimestamp(mktime(cal.parse(time_str)[0]))
time_formatted = dt.strftime('%b %d, %Y %H:%M')
print(time_formatted) # will print something like: Dec 15, 2013 02:10
Also see this question: python 'x days ago' to datetime

Related

How to convert datetime string to datetime object in python [duplicate]

This question already has answers here:
How to convert local time string to UTC?
(25 answers)
datetime from string in Python, best-guessing string format
(4 answers)
Closed 11 months ago.
I am converting a datetime object to string using:
from datetime import datetime
dt = datetime.now()
dt_str = str(dt)
# Now I want to get `dt`(datetime object) back from `dt_str`
How to convert dt_str to dt?
Can anyone help me with this?
Use strptime method. Example:
from datetime import datetime
date_string = "2020-03-14"
date_object = datetime.strptime(date_string, "%Y-%m-%d")
%Y - Year in four digits. Example: 2018, 2019 etc.
%m - Month as a zero-padded decimal number. Example: 01, 02, ..., 12
%d - Represents the day of the month. Example: 01, 02, ..., 31

How to subtract days from a date using datetime [duplicate]

This question already has answers here:
How to perform arithmetic operation on a date in Python?
(2 answers)
Closed 2 years ago.
Something surely extremely simple, but I've been browsing around for almost one hour and couldn't find:
Working with Python, I have a date d="2020-01-22" (means January, 22nd, 2020) and I want to calculate the date corresponding to d - 57 days. With datetime, surely, but how, exactly?
Use the following code-
from datetime import datetime, timedelta
d = datetime.today()
new_d = d - timedelta(days=57)
Use package datetime.
# python3
import datetime
d = datetime.datetime.strptime("2020-01-22", '%Y-%m-%d')
print(d - datetime.timedelta(days=57)) # 2019-11-26 00:00:00
You can use datetime.strptime. this is the main function for parsing strings into datetimes. It can handle all type of formats, with the format determined by a format string you provide. You can read in detail here
from datetime import datetime
date_time= datetime.strptime('2020-01-22", '%Y-%m-%d')
print(date_time)

How to format time.time() using strftime() in python [duplicate]

This question already has answers here:
Convert timestamp since epoch to datetime.datetime
(3 answers)
Closed 3 years ago.
I have an epoch time that is in Seconds.PartOfSecond format e.g. 1581218900.17436. How can this be used within time.strftime() ?
import time
curTime = time.time()
# Following is not correct but gives idea what i'm looking for
formattedTime = time.strftime('%A %B %e, %Y %t', curTime)
Update The answer this has been closed for is not imo an obvious replacement: how would one know we need to convert to datetime.datetime? The answer below is more to the point.
You need pass your second to gmtime
import time
s=time.gmtime(1581218900.17436)
time.strftime("%Y-%m-%d %H:%M:%S", s)
'2020-02-09 03:28:20'
time.strftime('%A %B %e, %Y %t', s)
'Sunday February 9, 2020 \t'

Converting Datetime string with non-zero padded day [duplicate]

This question already has answers here:
Whats wrong with my datetime.strptime format?
(1 answer)
datetime.strptime strange behavior [duplicate]
(2 answers)
Closed 3 years ago.
I am trying to convert a string to datetime object in python. Some examples of the string includes "Fri Oct 20 2006 4:25pm EDT" and "Wed Nov 1 2006 4:47pm EST" The problem seems to be that the day is not zero-padded which is why datetime does not pick up the format
I have also tried slicing it to convert the first 15 characters only but it seems that the non-zero padded dates are still a problem.
import datetime
for i, row in df.iterrows():
datetime_list.append(datetime.datetime.strptime(row['Time Stamp'],
'%a %b %d %Y %I:%M%p %Z'))
I hope to get a datetime object out that only shows the year, month and date.

Days between dates [duplicate]

This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
How do I parse an HTTP date-string in Python?
(4 answers)
Closed 5 years ago.
I need count days to password change in linux.
I know how to do this in python e.g:
>>> import datetime
>>> start = datetime.date(2016,1,1)
>>> end = datetime.date(2016,2,28)
>>> end-start
But my date format is:
Oct 03, 2017
How I can calculate days to a date?
You can do this using strftime().
It is actually really simple:
from datetime import datetime
d1 = datetime.strptime("Jan 01, 2016", '%b %d, %Y')
d2 = datetime.strptime("Feb 28, 2016", '%b %d, %Y')
print "Delta (in days):", (d2-d1).days
And you'll get 58 as a result.

Categories