Home > Software engineering >  How to find the amount of minutes between the current time and a future time of the same day in pyth
How to find the amount of minutes between the current time and a future time of the same day in pyth

Time:10-20

I'm trying to make a program that tells me how long there is (in minutes) until a certain time of day in the future. I've been trying to write working code all night, but I just can't wrap my head around strftime, timedelta and datetime. I'm quite new to python, and being able to do this would be quite useful to my day-to-day life; Can someone help me out?

CodePudding user response:

from datetime import datetime

def minutes_until(hour, minute):
    #get the current time
    now = datetime.now()
    #get the current hour
    current_hour = now.hour
    #get the current minute
    current_minute = now.minute
    #get the current second
    current_second = now.second
    #get the current microsecond
    current_microsecond = now.microsecond
    #get the time until the specified hour
    time_until_hour = hour - current_hour
    #get the time until the specified minute
    time_until_minute = minute - current_minute
    #get the time until the specified second
    time_until_second = 60 - current_second
    #get the time until the specified microsecond
    time_until_microsecond = 1000000 - current_microsecond
    #get the total time until the specified time
    total_time = time_until_hour * 3600   time_until_minute * 60   time_until_second   time_until_microsecond / 1000000
    #get the total time in minutes
    total_time_in_minutes = total_time / 60
    #return the total time in minutes
    return total_time_in_minutes

print(minutes_until(15, 0)) #time now is 2 PM, how long until 15:00 (3PM)? = 60 minutes

CodePudding user response:

You can try:

from datetime import datetime

target = '2023-01-01 00:00:00'

t = datetime.strptime(target, '%Y-%m-%d %H:%M:%S')
now = datetime.now()
print(f'{(t-now).total_seconds()/60:.0f} minutes')

output: 104445 minutes

  • Related