Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find monday of current week in Python [duplicate]

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

like image 885
Aleks Avatar asked Jan 30 '20 08:01

Aleks


People also ask

How do you get the Monday of the week in Python?

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.

How do you check if it's Monday in Python?

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.

How do I get the current week in Python?

Use get_week_dates(date. today(), 1, 7) to get current week dates.

How do you get next Monday in Python?

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).


1 Answers

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
like image 127
Mihai Alexandru-Ionut Avatar answered Nov 15 '22 07:11

Mihai Alexandru-Ionut