0

I have a datetime object and i want to add 6 hours for example. How does it works?

When its 2019-04-17 22:00:00 it should result the new datetime 2019-04-18 04:00:00.

I tried

date2 = '2019-04-17 22:00:00' date = datetime.datetime(date2) + timedelta(hours=6) 

but it doesn't work.

3
  • hours=6 for sure.. Commented Apr 18, 2019 at 15:43
  • 1
    Is date2 a string or a datetime.datetime object? Commented Apr 18, 2019 at 15:45
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. StackOverflow is not a coding, review, or tutorial resource. Commented Apr 18, 2019 at 16:12

4 Answers 4

1

assuming your date2 is a string, then you need to convert it to datetime object

import datetime date2 = '2019-04-17 22:00:00' date2_object = datetime.datetime.strptime(date2,'%Y-%m-%d %H:%M:%S') date = date2_object + datetime.timedelta(hours=6) 

if date2 is already a datetime object, you can simply do:

date = date2 + datetime.timedelta(hours=6) 
Sign up to request clarification or add additional context in comments.

Comments

0

Not sure what's in your 'date2' variable but

date = datetime.now() + timedelta(hours=6) 

worked perfectly fine for me

Comments

0

Assuming the dt is a datetime object:

from datetime import datetime, timedelta dt = datetime(2019, 4, 17, 22, 0, 0) print(dt + timedelta(hours=6)) 

OUTPUT:

2019-04-18 04:00:00 

2 Comments

I think this is the right answer, but if date is given as a string, just use dt = datetime.strptime("2019-04-17 22:00:00", "%Y-%m-%d %H:%M:%S")
How can I get a datetime object in the format like (2019, 4, 17, 22, 0, 0)?
-1

A complete datetime increasing algorithm that I wrote:

 var d=new Date(); var date=d.toISOString().split('T')[0]; var time=d.toISOString().split('T')[1].split(".")[0]; var yr=parseInt(date.split("-")[0]); var mo=parseInt(date.split("-")[1]); var da=parseInt(date.split("-")[2]); var hr=parseInt(time.split(":")[0]); var min=parseInt(time.split(":")[1]); var sec=parseInt(time.split(":")[2]); console.log("time before"); console.log(`${yr}-${mo}-${da} ${hr}:${min}:${sec}`); //this is the increse time function //first parameter is a string. Possible values: "sec", "min", "hr", "da" //second parameter is an integer. It is the value representing how much you want to increase //here we are increasing time by 52345 minutes... increaseTime("min", 52345); console.log("time after"); console.log(`${yr}-${mo}-${da} ${hr}:${min}:${sec}`); function increaseTime(whichOne, howHuch) { if(whichOne=="sec") sec=sec+howHuch; if(whichOne=="min") min=min+howHuch; if(whichOne=="hr") hr=hr+howHuch; if(whichOne=="da") da=da+howHuch; while(sec>59) { sec=sec-60; min=min+1; }; while(min>59) { min=min-60; hr=hr+1; }; while(hr>23) { hr=hr-24; da=da+1; }; while(da > getNumOfDaysFromMonth(mo, yr) ) { da=da-getNumOfDaysFromMonth(mo, yr); mo=mo+1; while(mo>12) { mo=mo-12; yr=yr+1; }; }; }; function getNumOfDaysFromMonth(mo, yr) { if(mo===1||mo===3||mo===5||mo===7||mo===8||mo===10||mo===12) { return 31; } else if(mo===4||mo===6||mo===9||mo===11) { return 30; } else if(mo===2) { //checking if leap yr if( ((yr%4==0)&&(yr%100!=0))||(yr%400==0) ) return 29; else return 28; } else { console.log("Error in month: "+mo); } }; 

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.