2

http://docs.python.org/2/library/datetime.html

I use the today function to get current date, how to perform simple arithmetic on this object so that I can reduce or increase the current date by 1 or 2 (i.e. yesterday or day-before yesterday)

2
  • You may be interested by the dateutil python library Commented Jan 15, 2013 at 10:03
  • datetime has timedelta module. You can use it. Commented Jan 15, 2013 at 10:32

1 Answer 1

4

you can do the following

In [6]: import datetime

# Today
In [7]: datetime.datetime.now()
Out[7]: datetime.datetime(2013, 1, 15, 15, 31, 10, 760000)

# Tomorrow's date
In [8]: print datetime.datetime.now() + datetime.timedelta(days=1)
2013-01-16 15:31:15.336000

# Day after tomorrow's day
In [9]: print datetime.datetime.now() + datetime.timedelta(days=2)
2013-01-17 15:31:52.601000

# Yesterday's date
In [10]: print datetime.datetime.now() - datetime.timedelta(days=1)
2013-01-14 15:32:00.081000

also works with datetime.datetime.today()

In [15]: datetime.datetime.today()
Out[15]: datetime.datetime(2013, 1, 15, 15, 34, 37, 55000)

In [16]: datetime.datetime.today() + datetime.timedelta(days=1)
Out[16]: datetime.datetime(2013, 1, 16, 15, 34, 47, 560000)
Sign up to request clarification or add additional context in comments.

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.