Python retrieves DayOfWeek as integer
If you are using datetime.datetime
then use method datetime.datetime.weekday
:
>>> d = datetime.datetime.now()
>>> d
datetime.datetime(2014, 11, 24, 22, 47, 3, 80000)
>>> d.weekday() # Monday
0
>>> (d.weekday() + 1) % 7
1
You need (... + 1)% 7 because the method returns 0 for Monday, 6 for Sunday.
UPDATE
You can also use datetime.datetime.isoweekday
that return 1 for Monday, 7 for Sunday.
>>> d.isoweekday() % 7
1
source to share
I am a little confused by your wording:
(0 for Sunday to 6 on Monday) # what is Monday 6?
But I'm wondering if you want isoweekday
:
>>> from datetime import datetime
>>> d = datetime.now()
>>> d.isoweekday()
1 # Monday
Edit
In light of your wish for Sunday to be 0, you should go with falsetru's answer (after editing):
>>> d.isoweekday() % 7
Or just convert Sunday to 0 elsewhere in your code.
source to share
Python uses 0 for Monday. See the weekday documentation .
import datetime
now = datetime.datetime(2014, 11, 23)
print now
print now.weekday()
It's Sunday, so it prints this:
2014-11-23 00:00:00
6
This is an example for Monday:
import datetime
now = datetime.datetime.now()
print now
print now.weekday()
It's Monday, so it prints this:
2014-11-24 07:47:19.827000
0
source to share