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.
Yes, DateDiff("h" is for hours. DateDiff("n" is for minutes, DateDiff("s" is for seconds.
^ _ ^
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.
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:
What I want is:
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.
Thank you, I never thought of working with the whole thing in minutes and converting it, I'll give that a try.
Thank you for marking my answer as correct. I do appreciate it.
^ _ ^
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????
^ _ ^
<cfset cc_TIC_cooler_good_untilDiff_in_minutes = dateDiff("n", now(), cc_TIC_cooler_good_until)>
<cfset numberOfMinutes=cc_TIC_cooler_good_untilDiff_in_minutes - 60*int(cc_TIC_cooler_good_untilDiff_in_minutes/60)>
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.)
^ _ ^
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()!
^ _ ^
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.
^ _ ^