Cleanest and most Pythonic way to get tomorrow's date?

Question:

What is the cleanest and most Pythonic way to get tomorrow’s date? There must be a better way than to add one to the day, handle days at the end of the month, etc.

Asked By: linkmaster03

||

Answers:

datetime.date.today() + datetime.timedelta(days=1) should do the trick

Answered By: Kamil Szot

timedelta can handle adding days, seconds, microseconds, milliseconds, minutes, hours, or weeks.

>>> import datetime
>>> today = datetime.date.today()
>>> today
datetime.date(2009, 10, 1)
>>> today + datetime.timedelta(days=1)
datetime.date(2009, 10, 2)
>>> datetime.date(2009,10,31) + datetime.timedelta(hours=24)
datetime.date(2009, 11, 1)

As asked in a comment, leap days pose no problem:

>>> datetime.date(2004, 2, 28) + datetime.timedelta(days=1)
datetime.date(2004, 2, 29)
>>> datetime.date(2004, 2, 28) + datetime.timedelta(days=2)
datetime.date(2004, 3, 1)
>>> datetime.date(2005, 2, 28) + datetime.timedelta(days=1)
datetime.date(2005, 3, 1)
Answered By: Mark Rushakoff

No handling of leap seconds tho:

>>> from datetime import datetime, timedelta
>>> dt = datetime(2008,12,31,23,59,59)
>>> str(dt)
'2008-12-31 23:59:59'
>>> # leap second was added at the end of 2008, 
>>> # adding one second should create a datetime
>>> # of '2008-12-31 23:59:60'
>>> str(dt+timedelta(0,1))
'2009-01-01 00:00:00'
>>> str(dt+timedelta(0,2))
'2009-01-01 00:00:01'

darn.

EDIT – @Mark: The docs say “yes”, but the code says “not so much”:

>>> time.strptime("2008-12-31 23:59:60","%Y-%m-%d %H:%M:%S")
(2008, 12, 31, 23, 59, 60, 2, 366, -1)
>>> time.mktime(time.strptime("2008-12-31 23:59:60","%Y-%m-%d %H:%M:%S"))
1230789600.0
>>> time.gmtime(time.mktime(time.strptime("2008-12-31 23:59:60","%Y-%m-%d %H:%M:%S")))
(2009, 1, 1, 6, 0, 0, 3, 1, 0)
>>> time.localtime(time.mktime(time.strptime("2008-12-31 23:59:60","%Y-%m-%d %H:%M:%S")))
(2009, 1, 1, 0, 0, 0, 3, 1, 0)

I would think that gmtime or localtime would take the value returned by mktime and given me back the original tuple, with 60 as the number of seconds. And this test shows that these leap seconds can just fade away…

>>> a = time.mktime(time.strptime("2008-12-31 23:59:60","%Y-%m-%d %H:%M:%S"))
>>> b = time.mktime(time.strptime("2009-01-01 00:00:00","%Y-%m-%d %H:%M:%S"))
>>> a,b
(1230789600.0, 1230789600.0)
>>> b-a
0.0
Answered By: PaulMcG

Even the basic time module can handle this:

import time
time.localtime(time.time() + 24*3600)
Answered By: u0b34a0f6ae

For people who are dealing with servers Time Stamp

To get yesterday Time Stamp:

yesterdaytimestamp = datetime.datetime.today() + datetime.timedelta(days=-1)

To get Today Time Stamp:

currenttimestamp = datetime.datetime.now().timestamp()

To get Tomorrow Time Stamp:

tomorrowtimestamp = datetime.datetime.today() + datetime.timedelta(days=1)

To print:

print('n Yesterday TimeStamp is : ', yesterdaytimestamp.timestamp(), 
    'n Today TimeStamp is :', currenttimestamp, 
    'n Tomorrow TimeStamp is: ', tomorrowtimestamp.timestamp())

The output:

Yesterday TimeStamp is :  1632842904.110993 
Today TimeStamp is       :  1632929304.111022 
Tomorrow TimeStamp is    :  1633015704.11103
Answered By: Mr Dream

There’s nothing at all wrong with using today() as shown in the selected answer if that is the extent of your needs.

datetime.date.today() + datetime.timedelta(days=1)

Alternatively, if you or someone else working with your code might need more precision in handling tomorrow’s date, consider using datetime.now() instead of today(). This will certainly allow for simpler, more readable code:

datetime.datetime.now() + datetime.timedelta(days=1)

This returns something like:

datetime.datetime(2022, 2, 17, 19, 50, 19, 984925)

The advantage is that you can now work with datetime attributes in a concise, human readable way:

class datetime.datetime

A combination of a date and a time. Attributes: year, month, day, hour, minute, second, microsecond, and tzinfo.

Examples

You can easily convert this to a date object withdate():

import datetime
tomorrow = datetime.datetime.now() + datetime.timedelta(days=1)
print(f"Tomorrow's date is {tomorrow.date()}")

tomorrow.date() is easy to use and it is very clear to anyone reading your code that it is returning the date for tomorrow. The output for the above looks like so:
Tomorrow's date is 2022-02-17

If later in your code you only need the date number for the day, you can now use tomorrow.day:

print(f"Tomorrow is the {tomorrow.day}rd")

Which will return something like:
Tomorrow is the 17rd

That’s a silly example, but you can see how having access to these attributes can be useful and keep your code readable as well. It can be easily understood that tomorrow.day returns the day number.

Need to work with the exact time tomorrow’s date begins? You can now replace the hours, minutes, seconds, and microseconds:

# Replace all attributes except day with 0.
midnight = tomorrow.replace(
    hour=0,
    minute=0,
    second=0,
    microsecond=0)
# Print midnight as the beginning of tomorrow's date.
print(f"{midnight}")

Reading the above code, it should be apparent which attributes of tomorrow are being replaced. When midnight is printed, it will output:
2022-02-17 00:00:00

Need to know the time left until tomorrow’s date? Now something like that is possible, simple, and readable:

print(f"{midnight - datetime.datetime.now()}")

The output is the time to the microsecond that tomorrow’s date begins:
3:14:28.158331

There are many ways people might wish to handle tomorrow’s date. By ensuring these attributes are available from the beginning, you can write more readable code and avoid unnecessary work later.

Answered By: iyrin

For the case you only want to calculate the timestamp

import time
tomorrow = (int(time.time() / 86400) + 1) * 86400
Answered By: MattPatrik
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.