Trying to compare a time string ('hh:mm:ss') that ...
# cfml-beginners
a
Trying to compare a time string ('hhmmss') that I receive - to the current time (any date parts are irrelevant). Looking at the CF date and time functions - didn't see anything that will compare times only. I guess I need to call CreateDateTime() - passing in the date parts from now(), plus my time parts - and then compare that constructed date with now(). Is this is scenario where I'd be better off dropping down to Java? say
Copy code
<cfscript>
    jTime = createObject( "java", "java.time.LocalTime" )
    jEnum = createObject("java", "java.time.temporal.ChronoUnit");
    variables.time1 = jTime.now();
    variables.time2 = jTime.parse('17:28:51');
    echo(variables.time1.until(variables.time2, jEnum.seconds));
</cfscript>
Method gets called pretty often, otw I would not be sweating it. thanks!
m
Specifying the correct
datepart
in the native
DateDiff
function won't work for you? https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-c-d/DateDiff.html
c
@Marc Funaro I think @Anders Lars's point is that he is only working with times, not dates.
DateDiff
requires two dates. So, as he stated, he'd have to turn the time string into a date object (using the current date's month/day/year in order to use
DateDiff
, rather than being able to just work with times alone.
a
That should work, but does mean I have to make a call to CreateDateTime() passing in all the parts.. which seems kind of ugly.. and adding all the date pieces just to make a comparison of the time parts
Copy code
<cfscript>
    dNow = now();
    inputTime = '03:02:22';
    inputTimeArray = listToArray(inputTime, ':');
    newDate = createDateTime(dNow.year(), dNow.month(), dNow.day(), inputTimeArray[1], inputTimeArray[2], inputTimeArray[3]);
    echo(newDate);  
</cfscript>
m
I'd just roll my own TimeDiff() method that takes two strings or whatever, but yes, it needs to be a full date/time stamp.
👍 1
s
DateCompare works with time
You can also hack it by wrapping in
Int()
just need to use the whole datetime instead though
🙏 1
(i.e. `Int(datetime) lte Int(datetime)`\)
j
dateCompare seems to work with everything but quarters, is there a best practise to compare quarters? Say I wanted to display the last quarter, so right now I'd like to get "2022 Q4"
s
quarter(date)
🙃 1
Will give you the quarter number then just concat with the year
👍 1
j
feel like I could*ve found that myself, sorry 😄
s
haha, no problem!