I have a date column in csv file say Date
having dates in this format 04/21/2013
and I have one more column Next_Day
. In Next_Day
column I want to populate the date which comes immediately after the date mentioned in date column. For eg. if date column has 04/21/2013
as date then I want 04/22/2013
in Next_Day column.
We can use +1
in excel but I don't know how to perform this in Python.
Please help me in resolving this.
The only arithmetic operations that can be performed on datetime values are addition and subtraction. If a datetime value is the operand of addition, the other operand must be a duration.
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.
datetime includes two methods, strptime() and strftime(), for converting objects from strings to datetime objects and vice versa. strptime() can read strings with date and time information and convert them to datetime objects, and strftime() converts datetime objects back into strings.
Using datetime.timedelta
>>> import datetime >>> s = '04/21/2013' >>> d = datetime.datetime.strptime(s, '%m/%d/%Y') + datetime.timedelta(days=1) >>> print(d.strftime('%m/%d/%Y')) 04/22/2013
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With