Rajeev
Rajeev

Reputation: 46909

Python get start and end date of the week

How to get the start date and end date of a week, and also if the following dates below comes under a particular range of start and end dates of the week how to show only those weeks start and end date.I am using python 2.4

2011-03-07  0:27:41
2011-03-06  0:13:41
2011-03-05  0:17:40
2011-03-04  0:55:40
2011-05-16  0:55:40
2011-07-16  0:55:40

Upvotes: 1

Views: 4499

Answers (2)

Senthil Kumaran
Senthil Kumaran

Reputation: 56823

From python2.5 onwards datetime module has datetime.strptime(date_string, format) which you can pass the date_string and format to get a datetime object for doing comparisions. In python2.4, you could use the time module's support of strptime. From the documentation, you should be able to find the week part of your question.

Upvotes: 0

Fred Foo
Fred Foo

Reputation: 363517

from datetime import datetime
from time import strptime

Now

datetime(*strptime('2011-03-08  0:27:41', '%Y-%m-%d  %H:%M:%S')[0:6]).weekday()

returns the day of the week for the first date "as an integer, where Monday is 0 and Sunday is 6", so selecting those dates for which weekday() in [0, 6] will give you the start and end dates of weeks (or use 4 instead of 6 for work weeks).

Upvotes: 1

Related Questions