Jump to content

Hours:Minutes to decimal format


etling

Recommended Posts

I found myself often converting hours & minutes to a decimal format when creating invoices. This workflow simply does that calculation and copies it to the clipboard. Type in the keyword, then hours:minutes to get that amount of time in decimal (rounded to the nearest hundredth).

 

For example, 6 hours and 38 minutes:

 

dtime 6:38

 

Copies 6.63 to clipboard.

 

http://static.etling.com/assets/decimal_time_workflow.zip

Link to comment
  • 2 years later...

Also if you want to go further here is a whole command-line time calculator library I wrote in python some years ago.  The code above does the conversion to minutes rather than seconds but it's basically the same as the input_time, to_seconds and base_time functionality below.

 

def to_seconds(i):
	seconds=int(i[2])+(int(i[1])*60)+(int(i[0]) * 60 * 60)
	return seconds
	
def from_seconds(raw_seconds):
	seconds=abs(int(raw_seconds))
	minutes=0
	hours=0
	while seconds > 59:
		seconds=seconds-60
		minutes=minutes+1
	while minutes > 59:
		minutes=minutes-60
		hours=hours+1
	if raw_seconds<0:
		hours=hours*-1
	return [str(hours), str(minutes), str(seconds)]

def input_time():
	time = raw_input('enter time in h:m:s format: ')
	time_list=time.split(":")
	return time_list

def add(base_time):
	increase=input_time()
	return(from_seconds(to_seconds(base_time)+to_seconds(increase)))	

def subtract(base_time):
	decrease=input_time()
	return(from_seconds(to_seconds(base_time)-to_seconds(decrease)))
	
def multiply(base_time, multiplier):
	return(from_seconds(to_seconds(base_time)*multiplier))

def divide(base_time, divisor):
	return(from_seconds(to_seconds(base_time)/divisor))

def milePaceToKMPace(mile_pace):
	return(multiply(mile_pace,0.621371))
	
def kmPacetoMilePace(km_pace):
	return(multiply(km_pace,1.609343994))

def decimal_equivalent(base_time):
	return(to_seconds(base_time)/3600.0)
	
def negative(base_time):
	return(multiply(base_time,-1))

def display_time(i):
	if len (i[1])==1:
		i[1]="0"+i[1]
	if len (i[2])==1:
		i[2]="0"+i[2]
	print i[0]+":"+i[1]+":"+i[2]

 

Edited by dfay
Link to comment
  • 2 months later...
On 2/16/2017 at 11:28 AM, dfay said:

Try this in a Run Script box with language as python and input as {query}.  You can pipe it out to a Copy to Clipboard action.


i = "{query}".split(":")

m=int(i[1])+(int(i[0])*60)

print float(m)/60
 

I got this to work and thank you so much, I was sad when the file here was a dead link. Can you explain how to make a script that will go the other way? hundereths to hh:m 

Link to comment
  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...