2
  1. I want to create time that is based on Year-Month-day. Lets call it a_date.

  2. I want to take current time with for example datetime.now() and format it to same format above. Lets call it b_date.

  3. I want to tell if b_date <= a_date.

How do I do that? I tried several things with time.mktime() and strftime, but I'm little confused.

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
JavaSa
  • 5,813
  • 16
  • 71
  • 121
  • 1
    Possible duplicate of [How to compare two dates?](http://stackoverflow.com/questions/8142364/how-to-compare-two-dates) – MrHug Jan 13 '16 at 15:59
  • Basically if both a_date and b_date are strings, then if b_date is older date then a_date, string comparison should give you direct answer because of the YYYY-MM-DD format! – rohitkulky Jan 13 '16 at 16:09

3 Answers3

4
import datetime

# year, month, day
a_date = datetime.date(2014, 11, 1)
b_date = datetime.date.today()

print(b_date)          # 2016-01-13
print(a_date < b_date) # true
dfrib
  • 70,367
  • 12
  • 127
  • 192
-1

try this:

def validitycheck():
    from datetime import datetime
    valid = datetime(2016, 1, 23)
    oldValid = datetime(2016, 1, 7)
    present = datetime.now()

    if not valid >= present or present <= oldValid:
        appValid = False
    else:
        appValid = True
    return appValid 
print validitycheck()
Bear
  • 550
  • 9
  • 25
-1
import datetime, calendar

def getTimestamp(a):
    return calendar.timegm(a.timetuple())

a_date = datetime.datetime(year, month, day)
b_date = datetime.datetime.now()
if getTimestamp(a_date) <= getTimestamp(b_date):
    # a_date <= b_date

References:
https://docs.python.org/2/library/datetime.html
https://docs.python.org/3.3/library/calendar.html#calendar.timegm

Rolbrok
  • 308
  • 1
  • 7