This question already has answers here:
Find Monday's date with Python
(8 answers)
How do I get the day of week given a date?
(30 answers)
Closed 5 years ago.
How would I write a statement that says:
If today is Monday, then run this function.
My thoughts are:
if datetime.now().day == Monday:
run_report()
But I know this is not the right way to do it. How would I properly do this?
You can use date.weekday() like this:
from datetime import date
# If today is Monday (aka 0 of 6), then run the report
if date.today().weekday() == 0:
run_report()
import datetime as dt
dt.date.today().isoweekday() == 1 # 1 = Monday, 2 = Tues, etc.
Both datetime.date and datetime.datetime
objects have a today method that return respectively a Date and a Datetime object.
Which both have a weekday and isoweekday methods.
weekday count from Monday = 0, while isoweekday count from Monday = 1:
from datetime import date, datetime
if date.today().weekday() == 0:
# it is Monday
if datetime.today().isoweekday() == 1:
# it is Monday
See documentation
Related
This question already has answers here:
Add 1 day to my date in Python [duplicate]
(1 answer)
Adding days to a date in Python
(16 answers)
Closed 2 years ago.
I'm trying to add one day to custom date(in string). Time format is dd/mm/yyyy
Sample input:
'02/11/2020'
Output:
'03/11/2020'
from datetime import datetime, timedelta
date = '02/11/2020'
old = datetime.strptime(date, "%d/%m/%Y")
new = old + timedelta(days=1)
new_date = new.strftime("%d/%m/%Y")
print(new_date) # '03/11/2020'
You can use the datetime module
from datetime import datetime, timedelta
start = datetime.strptime("%d/%m/%Y", "02/11/2020")
end = start + datetime.timedelta(days = 1)
This question already has answers here:
Getting the date of the first day of the week
(2 answers)
Python: give start and end of week data from a given date
(5 answers)
Closed 3 years ago.
I am trying to get the timestamp of monday at 00:00 of the current week in python. I know that for a specific date, the timestamp can be found using
baseTime = int(datetime.datetime.timestamp(datetime.datetime(2020,1,1)))
However, I want my program to automatically find out, based on the date, which date monday of the current week was, and then get the timestamp. That is to say, it would return different dates this week and next week, meaning different timestamps.
I know that the current date can be found using
import datetime
today = datetime.date.today()
Thanks in advance
I am trying to get the timestamp of monday at 00:00 of the current week in python
You could use timedelta method from datetime package.
from datetime import datetime, timedelta
now = datetime.now()
monday = now - timedelta(days = now.weekday())
print(monday)
Output
2020-01-27 08:47:01
This question already has answers here:
How do I check if it's Monday to Friday and the time is between 10 AM to 3 PM?
(3 answers)
Closed 3 years ago.
I'm finding this quite difficult and I'm not too sure which imports to use. Either way:
from datetime import datetime
from datetime import timedelta
from datetime import date
import holidays
def isTradingDay(theDate):
isWeekday = False
isBankHoliday = False
# Select country
holidayDates = holidays.UnitedKingdom()
#Check WeekDay and Bank Holiday status
if(datetime.today().weekday() < 6):
isWeekday = True
if(theDate in holidayDates):
isBankHoliday = True
#Return True if trading day
if(isWeekday == False and isBankHoliday == False):
return True
else:
return False
The issue lies with datetime.today().weekday() < 6 I want to use this line to check if the value theDate is a weekday. But I can't find the correct way of checking theDate.
theDate format is: (datetime.today() - timedelta(days = offset)).strftime('%d-%m-%Y')
Are there options that can be worked into the above?
If theDate is the datetime object, you can use .weekday() function too:
if(theDate.weekday() < 6):
If it is a string, you should first convert it to datetime with strptime:
dtime = datetime.strptime(theDate, '%Y-%m-%d')
'%Y-%m-%d' string depends on your string format of theDate
This question already has answers here:
How to increment a datetime by one day?
(8 answers)
Closed 4 years ago.
So basically I want to check if a certain string includes tommorows date so i made this date variable (see code).
Now the problem is every month on the last day this is going to be wrong. For example the 30th of september, with the way i did it, its going to say that tommorow will be the 31 of september, wich does not exist however. I need it to say that tommorow is the 1. of october.
Any suggestions? It has to be in the format dd.mm.yy please.
day = str(datetime.datetime.today().day+1)
month = str(datetime.datetime.today().month)
year = str(datetime.datetime.today().year)
date = day + "." + month + "." + year
just add one day to today
tomorrow = datetime.datetime.today() + datetime.timedelta(days=1)
print(str(tomorrow),tomorrow.strftime("%d.%m.%y"))
relativedelta can also be used
import datetime
from dateutil.relativedelta import relativedelta
tomorrow = datetime.datetime.today() + relativedelta(days=1)
print(str(tomorrow),tomorrow.strftime("%d.%m.%y"))
This question already has answers here:
How to increment datetime by custom months in python without using library [duplicate]
(21 answers)
Closed 9 years ago.
Is there any way to get from a date to the next period? I.e. I am looking for a funaction next that takes
now = datetime.datetime(2013, 11, 15, 0, 0)
to
next(now, 'D') = datetime.datetime(2013, 11, 16, 0, 0) #moving to next day
next(now, 'M') = datetime.datetime(2013,12,31) #moving to next month (day doesn't matter really)
I have tried using Day and MonthEnd from pandas.tseries.offsets but MonthEnd will convert to MonthEnd of the given month, not next month. Is there any simple way to do this?
EDIT: I know this is fairly easy for months. Or days. The problem is, what if I then decide to use business days (Alias 'B')? Or BusinessMonthEnd ('BM')? Surely there should be a method that works for any of these, without having to think about how to implement business days?
How about using dateutil, like this:
from datetime import date
from dateutil.relativedelta import relativedelta
one_day = date.today() + relativedelta (days =+ 1)
one_month = date.today() + relativedelta( months =+ 1 )
You should try the timedelta objects.
Finding the next day is pretty easy, as you just have to add 24 hours:
next=now+datetime.timedelta(hours=24)
The next week is simple as well with days=7, but the next month is a bit tricky because a timedelta with days=31 will sometimes fire you two months later.