Torb
Torb

Reputation: 279

How to add time to date time?

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.

Upvotes: 0

Views: 566

Answers (4)

Shadman Fatin
Shadman Fatin

Reputation: 180

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);
        }

  };

Upvotes: -1

Jzou
Jzou

Reputation: 1325

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)

Upvotes: 1

DirtyBit
DirtyBit

Reputation: 16792

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

Upvotes: 0

Klaas Ardinois
Klaas Ardinois

Reputation: 151

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

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

worked perfectly fine for me

Upvotes: 0

Related Questions