Python add minutes to timestamp

The title says it all. I am writing a script to make scheduled GET requests to an API. I want to print when the next API call will be made, which would be 15 minutes from the previous call.

I'm very close, but have been running into the following error: TypeError: a float is required

Here's my code:

import time, datetime
from datetime import datetime, timedelta

while True:
    ## create a timestamp for the present moment:
    currentTime = datetime.datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
    print "GET request @ " + str(currentTime)

    ## create a timestamp for 15 minutes into the future:
    nextTime = datetime.datetime.now() + datetime.timedelta(minutes = 15)
    print "Next request @ " + str(datetime.datetime.fromtimestamp(nextTime).strftime("%Y-%m-%d %H:%M:%S")
    print "############################ DONE #############################"
    time.sleep(900) ## call the api every 15 minutes   

I can get things to work (sort of) when changing the following line:

print "Next request @ " + str(nextTime)

However, this prints a timestamp with six decimal places for milliseconds. I want to keep things in the %Y-%m-%d %H:%M:%S format.

Seanny123

8,02312 gold badges65 silver badges118 bronze badges

asked Apr 3, 2017 at 19:17

Python add minutes to timestamp

philiporlandophiliporlando

8814 gold badges18 silver badges28 bronze badges

1

You don't need to use datetime.fromtimestamp since nextTime is already a datetime object (and not a float). So, simply use:

nextTime = datetime.datetime.now() + datetime.timedelta(minutes = 15)
print "Next request @ " + nextTime.strftime("%Y-%m-%d %H:%M:%S")

answered Apr 3, 2017 at 19:24

Python add minutes to timestamp

0

You can achieve it just by using timestamp instead:

import time
from datetime import datetime

while True:
    # create a timestamp for the present moment:
    currentTime_timestamp = time.time()
    currentTime = datetime.fromtimestamp(
        currentTime_timestamp
    ).strftime("%Y-%m-%d %H:%M:%S")
    print "GET request @ " + str(currentTime)

    # create a timestamp for 15 minutes into the future:
    nextTime = currentTime_timestamp + 900  # 15min = 900 seconds
    print "Next request @ " + str(datetime.fromtimestamp(
        nextTime
    ).strftime("%Y-%m-%d %H:%M:%S"))
    print "############################ DONE #############################"
    time.sleep(900)  # call the api every 15 minutes

The output I got was:

GET request @ 2017-04-03 16:31:34
Next request @ 2017-04-03 16:46:34
############################ DONE #############################

answered Apr 3, 2017 at 19:33

Joab MendesJoab Mendes

1,00018 silver badges32 bronze badges

Close. There is no need to use fromtimestamp here. You can reduce the last couple of lines to:

import datetime as dt

nextTime = dt.datetime.now() + dt.timedelta(minutes = 15)
print "Next request @ " + dt.datetime.strftime(nextTime, "%Y-%m-%d %H:%M:%S")

In other words, pass the datetime object nextTime as the first parameter to strftime.

answered Apr 3, 2017 at 19:24

roganjoshroganjosh

12k4 gold badges29 silver badges44 bronze badges

2

As soon as I asked this question I found out what I was doing wrong...

For those who are curious, here is the solution:

import time, datetime
from datetime import datetime, timedelta

while True:
    ## create a timestamp for the present moment:
    currentTime = datetime.datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
    print "GET request @ " + str(currentTime)

    ## create a timestamp for 15 minutes into the future:
    nextTime = datetime.datetime.now() + datetime.timedelta(minutes = 15)
    print "Next request @ " + str(nextTime.strftime("%Y-%m-%d %H:%M:%S"))
    print "############################ DONE #############################"
    time.sleep(900) ## call the api every 15 minutes

answered Apr 3, 2017 at 19:24

Python add minutes to timestamp

philiporlandophiliporlando

8814 gold badges18 silver badges28 bronze badges

How do you add minutes to a timestamp in Python?

Add minutes to a timestamp in Python using timedelta.
Step 1: If the given timestamp is in string format, then convert it to the datetime object using datetime. ... .
Step 2: Create an object of timedelta, to represent an interval of N minutes. ... .
Step 3: Add the timedelta object to the datetime object created in step 1..

How do I add minutes to a datetime?

Use the timedelta() class from the datetime module to add minutes to datetime, e.g. result = dt + timedelta(minutes=10) . The timedelta class can be passed a minutes argument and adds the specified number of minutes to the datetime.

How do I add hours and minutes to a date in Python?

Use the timedelta() class from the datetime module to add hours to datetime, e.g. result = dt + timedelta(hours=10) . The timedelta class can be passed a hours argument and adds the specified number of hours to the datetime.

Can you add time in Python?

Use the timedelta() class from the datetime module to add time to datetime, e.g. result = dt + timedelta(hours=2, minutes=25, seconds=24) .