date comparison in python - how to know which semester date -


i somehow stuck in logical thinking.

i going compare date 2 states:

  1. summer semester (beginns on 16.04 - ends on 14.10)
  2. winter semester (beginns on 15.10 - ends on 15.04)

all including vacations

how check semester month/day in?

current_month = datetime.datetime.now().month current_year = datetime.datetime.now().year current_day = datetime.datetime.now().day  if current_month>=10 , current_day>=15:    #winter semester 

but somehow doing chaos. there python lib date comparison problem?

you generate summer dates:

current = datetime.date.today() summer = current.replace(month=4, day=16), current.replace(month=10, day=14)  if summer[0] <= current <= summer[1]:     # summer semester else:     # winter semester 

this use datetime.date() object instead of datetime.datetime(), time portion irrelevant here. date.replace() calls make sure reuse current year.

this assumes, of course, summer semester starts , ends on same dates each year.

demo:

>>> import datetime >>> current = datetime.date.today() >>> current datetime.date(2013, 9, 3) >>> summer = current.replace(month=4, day=16), current.replace(month=10, day=14) >>> summer[0] <= current <= summer[1]: true 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -