-2

Have a maths question here which I know to solve using only pen and paper. Takes a while with that approach, mind you. Does anybody know to do this using Python? I've done similar questions involving "dates" but none involving "days". Any of you folk able to figure this one out?

The date on 25/11/1998 is a Wednesday. What is the day on 29/08/2030?

Can anyone at least suggest an algorithm?

Cheers

Cloud
  • 171
  • 1
  • 1
  • 8
  • Please don't use both [python-2.7] and [python-3.x] tags. If the version is irrelevant, use neither. – Wooble Sep 24 '13 at 23:49
  • Do you actually need an algorithm to do this yourself (for a class, because you need dates outside the limit of `str[pf]time`, whatever…)? Or is Haidro's answer all you need? – abarnert Sep 24 '13 at 23:52

2 Answers2

2

Use the wonderful datetime module:

>>> import datetime
>>> mydate = datetime.datetime.strptime('29/08/2030', '%d/%M/%Y')
>>> print mydate.strftime('%A')
Tuesday
TerryA
  • 58,805
  • 11
  • 114
  • 143
0

The algorithm/math is quite simple: There are always 7 days a week. Just calculate how many days between the two days, add it to the weekday of the given day then mod the sum by 7.

<!-- language: python -->
> from datetime import datetime
> given_day = datetime(1998,11,25)
> cal_day = datetime(2030,8,29)
> print cal_day.weekday()
 3
> print (given_day.weekday() + (cal_day-given_day).days) % 7
 3
Leonardo.Z
  • 9,425
  • 3
  • 35
  • 38