time and datetime modules
For some of your purposes, you can use the time module with strftime() or the date module with strftime() . This allows you to pull, among other things:
- week number of the year
- weekday number (you can also use the
weekday() method to get the number on weekdays between 0 for Monday and 6 for Sunday), - year
- month,
Which will be enough to calculate the first day of the month, the first day of the week and some other data.
Examples
To pull the necessary data, do the following:
to display the day number of the week
>>> from datetime import datetime >>> datetime.now().weekday() 6
to use the replace() function of the datetime object on the first day of the month:
>>> from datetime import datetime >>> datetime.now() datetime.datetime(2012, 3, 3, 21, 41, 20, 953000) >>> first_day_of_the_month = datetime.now().replace(day=1) >>> first_day_of_the_month datetime.datetime(2012, 3, 1, 21, 41, 20, 953000)
EDIT . As suggested by Yu.F. Sebastian in the comments, datetime objects have weekday() methods, which makes using int(given_date.strftime('%w')) pretty pointless. I updated the answer above.
source share