Jump to content
etling

Hours:Minutes to decimal format

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

Share this post


Link to post

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
Edited by dfay

Share this post


Link to post

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

Share this post


Link to post
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 

Share this post


Link to post

FWIW, you should never use : in a filename that's intended for a Mac. The old macOS used : as a path separator, not /, so using a colon in a filename can cause unexpected behaviour.

 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...