2

I need to convert this result to timestamp:

>>> print (datetime.date(2010, 1, 12) + datetime.timedelta(days = 3))
2010-01-15

I need to compare the value with this timestamp:

>>> datetime.datetime.now()
2011-10-24 10:43:43.371294

How can I achieve this?

3 Answers 3

8

I need to convert this result to timestamp

import time


mydate = datetime.date(2010, 1, 12) + datetime.timedelta(days = 3)
time.mktime(mydate.timetuple())

I need to compare the value with this timestamp:

a = datetime.datetime(2010, 1, 12) + datetime.timedelta(days = 3)
b = datetime.datetime.now()

a < b 
a > b 
a == b 
Sign up to request clarification or add additional context in comments.

Comments

1
oneDate = datetime.date(2010, 1, 12) + datetime.timedelta(days = 3)
now = datetime.datetime.now()

The first is date, the second is datetime. So if you just want to compare the dates (day, month, year), convert the second to date:

oneDate < now.date()

returns True

2 Comments

Hi, tahnks for the reply. I got the error when try to compare: can't compare datetime.datetime to datetime.date
You really need to call date() on now, otherwise you'll get that error.
1

datetime.datetime.now() will return an instance of datetime.datetime which has a date() method returning an instance of datetime.date. You can then compare that to the result of datetime.date(2010, 1, 12) + datetime.timedelta(days = 3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.