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
isoweekday() to get a weekday of a given date in Python Use the isoweekday() method to get the day of the week as an integer, where Monday is 1 and Sunday is 7. i.e., To start from the weekday number from 1, we can use isoweekday() in place of weekday() . The output is 1, which is equivalent to Monday as Monday is 1.
from datetime import datetime # If today is Monday (0 = Mon, 1 = Tue, 2 = Wen ...) if datetime. today(). weekday() == 0: print("Yes, Today is Monday") else: print("Nope...") from datetime import datetime # If today is Monday (1 = Mon, 2 = Tue, 3 = Wen ...) if datetime.
Use get_week_dates(date. today(), 1, 7) to get current week dates.
You can find the next Monday's date easily with Python's datetime library and the timedelta object. You just need to take today's date. Then subtract the number of days which already passed this week (this gets you 'last' monday).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With