# AppleScript : convert decimal to hours / days

How can I convert the value on my script from decimal to hours / or day?

For converting decimal to hour I believe it's :

hours + (minutes /60)

but my variable is in this format :

23.498611111111

Somehow I have to get 23 set as a value (hours) and same for the two decimal after the "." but I'm not sure how to proceed

here is my full script:

``````set myVariableDateHere to "25/03/2019 00:00:00"
set myWantedDate to convertDate(myVariableDateHere)

to convertDate(textDate)
set resultDate to the current date

set the day of resultDate to (text 1 thru 2 of textDate)
set the month of resultDate to (text 4 thru 5 of textDate)
set the year of resultDate to (text 7 thru 10 of textDate)
set the time of resultDate to 0

if (length of textDate) > 10 then
set the hours of resultDate to (text 12 thru 13 of textDate)
set the minutes of resultDate to (text 15 thru 16 of textDate)

if (length of textDate) > 16 then
set the seconds of resultDate to (text 18 thru 19 of textDate)
end if
end if

return resultDate
end convertDate

set myTimeDifResult to ((current date) - myWantedDate as string)
set daysV to myTimeDifResult / 86400
set hoursV to myTimeDifResult / 60 / 60
``````

set hoursV to roundThis(hoursV, 2) --> -75.54 on roundThis(n, numDecimals) set x to 10 ^ numDecimals tell n * x if ((it mod 2) ^ 2 > 0.25) then return (it div 0.5 - it div 1) / x return it div 1 / x end tell end roundThis

``````set final to hoursV

if hoursV > 24 then
set final to (daysV as integer) - 1
end if

return final
``````

update :

I figured out how two set both variable :

``````set the_decimalMin to hoursV mod 1
set the_decimaHours to hoursV div 1
``````

but now I can't make the actual math :

``````set hoursV to the_decimaHours + (the_decimalMin / 60)
``````

error :

error "Can’t make the_decimalMin into type reference." number -1700 from MyMinutes to reference

• Please make sure your code is indented properly — there is a section of code not within code formatting in the middle of your post, which makes the code hard to follow.
– grg
Mar 27, 2019 at 8:01
• You can avoid a lot of your overcomplication by using integer and modulo math. What kind of output are you trying to get - days, hours, minutes? Mar 27, 2019 at 14:20
• I’m trying to get the time difference between two date in days hours and minutes or hours / minutes only regarding the result Mar 27, 2019 at 14:29

The AppleScript constants `days`, `hours`, and `minutes` are the number of seconds in a day, hour, and minute respectively, so you can just perform some modulo math to get the components. For example, the hour would be the number `mod days div hours`, the minute would be the number `mod hours div minutes`, and the seconds would be the number `mod minutes`.

Putting all that together into a few general-purpose handlers would be something like:

``````set myVariableDateHere to "25/03/2019 00:00:00"
set myWantedDate to convertDate(myVariableDateHere)
log result
set difference to (current date) - myWantedDate
log result
log timeString(difference)
log formatTime(difference)

# Convert date text to an AppleScript date.
# format is DD/MM/YYYY hh:mm:ss (time is optional)
to convertDate(dateText)
set dateTemplate to (current date)
tell dateTemplate to set {its day, its month, its year} to words 1 thru 3 of dateText
set time of dateTemplate to 0
tell words of dateText to if (count it) > 3 then set time of dateTemplate to (hours * (item 4)) + (minutes * (item 5)) + (item 6)
return dateTemplate
end convertDate

# Return an elapsed time string from a number of seconds.
on timeString(theSeconds)
if class of theSeconds is integer then set theSeconds to "" & ¬
(theSeconds div days) & " days, " & ¬
(theSeconds mod days div hours) & " hours, " & ¬
(theSeconds mod hours div minutes) & " minutes, and " & ¬
(theSeconds mod minutes) & " seconds"
return theSeconds
end timeString

# Return a formatted time string from a number of seconds.
# format is hh:mm:ss (wraps at 24 hours)
to formatTime(theSeconds)
if class of theSeconds is integer then tell "0" & ¬
(10000 * (theSeconds mod days div hours) ¬
+ 100 * (theSeconds mod hours div minutes) ¬
+ (theSeconds mod minutes)) ¬
to set theSeconds to (text -6 thru -5) & ":" & (text -4 thru -3) & ":" & (text -2 thru -1)
return theSeconds
end formatTime
``````
• wow that's perfect thank you ! Mar 27, 2019 at 18:13