fork download
  1. import datetime
  2. import calendar
  3.  
  4. def get_dates_in_weeks(number_of_weeks=None):
  5. """
  6. With this function, we can get list days which are contained in 1 week, 2 weeks...from today
  7. The results: [datetime.date(2014, 1, 1), datetime.date(2014, 1, 2), datetime.date(2014, 1, 3), datetime.date(2014, 1, 4), datetime.date(2014, 1, 5), datetime.date(2014, 1, 6), datetime.date(2014, 1, 7)]
  8. """
  9. if number_of_weeks is None:
  10. number_of_weeks = 1
  11.  
  12. days = number_of_weeks * 7
  13. today = datetime.date.today()
  14. weekday = today.weekday()
  15. start_delta = datetime.timedelta(days=weekday)
  16. start_of_week = today - start_delta
  17. week_dates = [start_of_week + datetime.timedelta(days=i) for i in range(days)]
  18. return week_dates
  19.  
  20. def get_dates_in_current_month():
  21. """
  22. Get all days in current month
  23. The results: [datetime.date(2014, 1, 1), datetime.date(2014, 1, 2), datetime.date(2014, 1, 3),..., datetime.date(2014, 1, 29), datetime.date(2014, 1, 30), datetime.date(2014, 1, 31)]
  24. """
  25. today = datetime.date.today()
  26. year = today.year
  27. month = today.month
  28. num_days = calendar.monthrange(year, month)[1]
  29. days = [datetime.date(year, month, day) for day in range(1, num_days+1)]
  30. return days
  31.  
  32. if __name__ == '__main__':
  33.  
  34. # Show days in a week
  35. print get_dates_in_weeks()
  36.  
  37. # Show days in 2 week
  38. print get_dates_in_weeks(2)
  39.  
  40. # Show days in current month
  41. print get_dates_in_current_month()
Success #stdin #stdout 0.02s 8608KB
stdin
Standard input is empty
stdout
[datetime.date(2014, 2, 17), datetime.date(2014, 2, 18), datetime.date(2014, 2, 19), datetime.date(2014, 2, 20), datetime.date(2014, 2, 21), datetime.date(2014, 2, 22), datetime.date(2014, 2, 23)]
[datetime.date(2014, 2, 17), datetime.date(2014, 2, 18), datetime.date(2014, 2, 19), datetime.date(2014, 2, 20), datetime.date(2014, 2, 21), datetime.date(2014, 2, 22), datetime.date(2014, 2, 23), datetime.date(2014, 2, 24), datetime.date(2014, 2, 25), datetime.date(2014, 2, 26), datetime.date(2014, 2, 27), datetime.date(2014, 2, 28), datetime.date(2014, 3, 1), datetime.date(2014, 3, 2)]
[datetime.date(2014, 2, 1), datetime.date(2014, 2, 2), datetime.date(2014, 2, 3), datetime.date(2014, 2, 4), datetime.date(2014, 2, 5), datetime.date(2014, 2, 6), datetime.date(2014, 2, 7), datetime.date(2014, 2, 8), datetime.date(2014, 2, 9), datetime.date(2014, 2, 10), datetime.date(2014, 2, 11), datetime.date(2014, 2, 12), datetime.date(2014, 2, 13), datetime.date(2014, 2, 14), datetime.date(2014, 2, 15), datetime.date(2014, 2, 16), datetime.date(2014, 2, 17), datetime.date(2014, 2, 18), datetime.date(2014, 2, 19), datetime.date(2014, 2, 20), datetime.date(2014, 2, 21), datetime.date(2014, 2, 22), datetime.date(2014, 2, 23), datetime.date(2014, 2, 24), datetime.date(2014, 2, 25), datetime.date(2014, 2, 26), datetime.date(2014, 2, 27), datetime.date(2014, 2, 28)]