Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the time of tomorrow 1:30 AM?

I’m using the following expression to get the time of 1:30 AM tomorrow.

import datetime

t = datetime.date.today()
datetime.datetime.combine(t, datetime.time.min) + datetime.timedelta(hours=1, days=1, minutes=30)

Is there a better way to do it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your way of writing is totally alright, another way of achieving same task is:

Try it online!

from datetime import datetime
now = datetime.now()
print(datetime(year = now.year, month = now.month,
    day = now.day + 1, hour = 1, minute = 30))

Output:

2021-11-28 01:30:00

Just figured out that the way of writing as above will not work for the last day of the month, then it is better to do as following:

Try it online!

import datetime
now = datetime.datetime.now()
print(datetime.datetime(year = now.year, month = now.month, day = now.day)
    + datetime.timedelta(days = 1, hours = 1, minutes = 30))

Output:

2021-11-28 01:30:00
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading