Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get tomorrow's date in Python?

Tags:

python

I am able to get today's date and time using a Python script. But I need to get tomorrow's date and time in number format. So I used the script below.

#! /usr/bin/python
import datetime
import sys
import os

tomorrow = datetime.date.today() + datetime.timedelta(days=1)
print "Tomorrow date is" + str(tomorrow) 
tm_stme = datetime.time(0, 0, 0)
tm_etime = datetime.time(23,59,59)
tm_stdate = datetime.datetime.combine(tomorrow, tm_stme)
tm_enddate = datetime.datetime.combine(tomorrow,tm_etime)

print "tomorrow start date:" + tm_stdate
print "tomorrow end date:" + tm_enddate  

tm_sdt_convert = time.mktime(time.strptime(tm_stdate, "%Y-%m-%d %H:%M:%S"))
tm_tdt_convert = time.mktime(time.strptime(tm_enddate, "%Y-%m-%d %H:%M:%S"))

But it throws the error below:

administrator@Ashok-Dev:~/Desktop$ python testing.py
Tomorrow date is2012-11-24
Traceback (most recent call last):
File "testing.py", line 13, in <module>
print "tomorrow start date:" + tm_stdate
TypeError: cannot concatenate 'str' and 'datetime.datetime' objects

I need to get tm_sdt_convert as a variable result. How do I get the output without error?

like image 534
Viswa Avatar asked Nov 23 '12 11:11

Viswa


People also ask

How do you get dd mm yyyy in Python?

Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.

How do I code a date in Python?

To create a date, we can use the datetime() class (constructor) of the datetime module. The datetime() class requires three parameters to create a date: year, month, day.

What does date () do in Python?

The date() instance method of the python datetime class returns a date instance. Using this method only the date information excluding the time information is retrieved from a datetime instance.

How do I get the latest date in Python?

Example 1: Python get today's datetoday() method to get the current local date. By the way, date. today() returns a date object, which is assigned to the today variable in the above program. Now, you can use the strftime() method to create a string representing date in different formats.


1 Answers

some thing like this can help you

In [125]: datetime.datetime.now()
Out[125]: datetime.datetime(2012, 11, 23, 17, 11, 15, 765000)

In [126]: str(datetime.datetime.now() + datetime.timedelta(days=1))
Out[126]: '2012-11-24 17:11:18.203000'

to get individual things

In [127]: (datetime.datetime.now() + datetime.timedelta(days=1)).day
Out[127]: 24

In [128]: (datetime.datetime.now() + datetime.timedelta(days=1)).year
Out[128]: 2012

In [129]: (datetime.datetime.now() + datetime.timedelta(days=1)).month
Out[129]: 11
like image 197
avasal Avatar answered Oct 30 '22 08:10

avasal