Copy link to clipboard
Copied
I am showing a datediff using just hours. Can I show hours and minutes? I am using CF10, and I am not a programmer.
Here is what I am using
<cfset cc_TIC_cooler_good_untilDiff = DateDiff("h", Now(), cc_TIC_cooler_good_until)> cc_TIC_cooler_good_until = date/time stored in database.
Thank you
According to calculatorsoup.com, a method (dubbed the alternative method) to calculate hours:minutes from minutes is to:
There are 60 minutes in an hour or 60 minutes per hour. Written mathematically as a value of 1 it is [60 min / 1 hr] = 1. The inverse is also true that [1 hr / 60 min] = 1
To convert minutes to hours and minutes by division and multiplication,
Copy link to clipboard
Copied
Yes, DateDiff("h" is for hours. DateDiff("n" is for minutes, DateDiff("s" is for seconds.
https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-c-d/DateDiff.html
V/r,
^ _ ^
UPDATE: I just noticed you said "hours and minutes". So, in that case you'd get the difference in minutes, and convert to hours:minutes using simple math.
Copy link to clipboard
Copied
Thanks, I do know that much.
What I want to show is a good until date time, and then in parenthesis after that how many hours and minutes that is.
What I have is:
12/28/2018 02:44
(11)
What I want is:
12/28/2018 02:44
(11:48)
Copy link to clipboard
Copied
And then just use floor() to round down to the nearest integer because 0.16667 * 60 doesn't equal 10, it equals something like 10.0002, or something.
Copy link to clipboard
Copied
According to calculatorsoup.com, a method (dubbed the alternative method) to calculate hours:minutes from minutes is to:
There are 60 minutes in an hour or 60 minutes per hour. Written mathematically as a value of 1 it is [60 min / 1 hr] = 1. The inverse is also true that [1 hr / 60 min] = 1
To convert minutes to hours and minutes by division and multiplication,
To show an example and how it works mathematically, let's say we want to convert 190 minutes to hours and minutes. We multiply by [1 hr / 60 min] which is 1. The min unit cancels out and our result is in hr units.
190 min * [1 hr / 60 min] = 190/60 hr = 3.16667 hr
3 is the hours part.
The minutes part is calculated as 0.16667 * 60 = 10
So, 190 minutes = 3 hours and 10 minutes.
Copy link to clipboard
Copied
Thank you, I never thought of working with the whole thing in minutes and converting it, I'll give that a try.
Copy link to clipboard
Copied
Thank you for marking my answer as correct. I do appreciate it.
V/r,
^ _ ^
Copy link to clipboard
Copied
OOOOOOH! I just learned something way cool.
You can do it using a modulus!!
Using the example above of 190 minutes:
190 / 60 = 3.16667 (3 is the hours, use floor() to round that down to just 3)
190 % 60 = 10 (the number of minutes without calculating or rounding down)
How cool is THAT????
V/r,
^ _ ^
Copy link to clipboard
Copied
<cfset cc_TIC_cooler_good_untilDiff_in_minutes = dateDiff("n", now(), cc_TIC_cooler_good_until)>
<cfset numberOfHours=int(cc_TIC_cooler_good_untilDiff_in_minutes/60)>
<cfset numberOfMinutes=cc_TIC_cooler_good_untilDiff_in_minutes - 60*int(cc_TIC_cooler_good_untilDiff_in_minutes/60)>
Copy link to clipboard
Copied
I just learned something EVEN MORE COOL!!!
According to Ben Nadel, ever since CF7 there has been a math function "\" that is "integer division". It does the same thing as using int() on the results (ie, it will automatically remove the decimal and everything after it.) So it is now even easier:
190 \ 60 = 3
190 % 60 = 10 (the number of minutes without calculating or rounding down - it's the REMAINDER.)
Mind. Blown.
V/r,
^ _ ^
Copy link to clipboard
Copied
I am aware of the arithmetic operators \ and % (mod). But I prefer int() as it is more intuitive and more widely known. The operators \ and % may appear short and crisp but, for simple tasks like the one here, they are no faster than int()!
Copy link to clipboard
Copied
They may not be faster, and may not be intuitive (although I'll argue mod v % as % being intuitive.. it just makes sense, to me; JavaScript has used % to represent modulus for quite a while), it's less code to write. That is typically enough, for me (although not always.)
V/r,
^ _ ^
Copy link to clipboard
Copied
WolfShade wrote
... it's less code to write.
Fair enough.
Copy link to clipboard
Copied
I know I'm kind of splitting hairs, here, but I just realised that I shouldn't have called it "modulus". I should have called it "remainder". Apparently, for positive values they are identical. But negative values are different between mod and rem.
https://stackoverflow.com/questions/13683563/whats-the-difference-between-mod-and-remainder
V/r,
^ _ ^