How to find the date n days ago in Python?
Good evening chaps,
I would like to write a script where I give python a number of days (let s call it d) and it gives me the date we were d days ago.
I am struggling with the module datetime:
import datetime tod = datetime.datetime.now() d = timedelta(days = 50) a = tod - h Type Error : unsupported operand type for - : "datetime.timedelta" and "datetime.datetime"
Thanks for your help
You have mixed something up with your variables, you can subtract timedelta
datetime.datetime.now() with no issue:
import datetime tod = datetime.datetime.now() d = datetime.timedelta(days = 50) a = tod - d print(a) 2014-12-13 22:45:01.743172
Getting the date of 7 days ago from current date in python, import datetime as DT today = DT.date.today() week_ago = today - DT.timedelta( days=7). Converting time zones for datetime objects in Python — posted 2009-05-05; Python datetime / time conversions — posted 2008-11-12; How to get the current date and time in Python — posted 2008-06-26
If your arguments are something like, yesterday,2 days ago, 3 months ago, 2 years ago. The function below could be of help in getting the exact date for the arguments. You first need to import the following date utils
import datetime from dateutil.relativedelta import relativedelta
Then implement the function below
def get_past_date(str_days_ago): TODAY = datetime.date.today() splitted = str_days_ago.split() if len(splitted) == 1 and splitted.lower() == 'today': return str(TODAY.isoformat()) elif len(splitted) == 1 and splitted.lower() == 'yesterday': date = TODAY - relativedelta(days=1) return str(date.isoformat()) elif splitted.lower() in ['hour', 'hours', 'hr', 'hrs', 'h']: date = datetime.datetime.now() - relativedelta(hours=int(splitted)) return str(date.date().isoformat()) elif splitted.lower() in ['day', 'days', 'd']: date = TODAY - relativedelta(days=int(splitted)) return str(date.isoformat()) elif splitted.lower() in ['wk', 'wks', 'week', 'weeks', 'w']: date = TODAY - relativedelta(weeks=int(splitted)) return str(date.isoformat()) elif splitted.lower() in ['mon', 'mons', 'month', 'months', 'm']: date = TODAY - relativedelta(months=int(splitted)) return str(date.isoformat()) elif splitted.lower() in ['yrs', 'yr', 'years', 'year', 'y']: date = TODAY - relativedelta(years=int(splitted)) return str(date.isoformat()) else: return "Wrong Argument format"
You can then call the function like this:
print get_past_date('5 hours ago') print get_past_date('yesterday') print get_past_date('3 days ago') print get_past_date('4 months ago') print get_past_date('2 years ago') print get_past_date('today')
How to get the date N days ago in Python, from datetime import datetime, timedelta N = 2 date_N_days_ago = datetime.now () - timedelta(days=N) print(datetime.now())� Python Basic: Exercise-14 with Solution. Write a Python program to calculate number of days between two dates. Python datetime.date(year, month, day) : The function returns date object with same year, month and day. All arguments are required. Arguments may be integers, in the following ranges: MINYEAR <= year <= MAXYEAR; 1 <= month <= 12
Below code should work
from datetime import datetime, timedelta N_DAYS_AGO = 5 today = datetime.now() n_days_ago = today - timedelta(days=N_DAYS_AGO) print today, n_days_ago
Python Exercise: Get the dates 30 days before and after from the , Python Exercises, Practice and Solution: Write a Python program to get the dates 30 days before and after from the current date. Python Current Date and Time: now() today() Step 1) Like Date Objects, we can also use "DATETIME OBJECTS" in Python. It gives date along with time in hours, minutes, seconds and milliseconds. When we execute the code for datetime, it gives the output with current date and time. Step 2) With "DATETIME OBJECT", you can also call time class.
Get Previous, Current and Next-Day System Dates in Python, Current Date: import datetime Current_Date = datetime.datetime.today() print (Current_Date) Previous Date: import datetime Previous_Date = datetime.datetime.today() - datetime.timedelta(days=1) print (Previous_Date) Previous: Write a Python program to create 12 fixed dates from a specified date over a given period. The difference between two dates will be 20. The difference between two dates will be 20. Next: Write a Python program to get the GMT and local current time.
python 'x days ago' to datetime, x minutes/hours/days/months/years ago. Now if I need You just need a timedelta and try the following : s = "3 days How to convert datetime.date to UTC timestamp in Python How to check the version of Python? you can� If you really just want the date, you can call the date method: >>> (datetime.datetime.now() - datetime.timedelta(days=7)).date() datetime.date(2013, 12, 6) Or, work with dates to begin with as suggested by unutbu.
Get the date n days ago and n days after in Python, Few day's ago I found a helpful method to get n day's ago date and n days after date from current date. Following functions are written based on� Today we are going to learn how to add days to date in Python. In my previous few tutorials, have shown you working with datetime in Python. Here are some links to my previous posts: How to subtract days from date in Python; How to calculate age in days from date of birth; So this time I will focus on Adding days to date. Add days to a specific
- What is
hsupposed to be?
- possible duplicate of How can I subtract a day from a python date?