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
Best Answer
The AppleScript constants
days
,hours
, andminutes
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 numbermod days div hours
, the minute would be the numbermod hours div minutes
, and the seconds would be the numbermod minutes
.Putting all that together into a few general-purpose handlers would be something like: