CodeHunt
CodeHunt

Reputation: 73

How to add two time in coldfusion

I am trying to find end time with duration and start time using ColdFusion. What I have tried is :

<cfset st_time=timeFormat("05:00:00",'hh:mm:ss tt')>
<cfset s_d=listToArray(duration,":")>
<cfset hours=s_d[1]>
<cfset min=s_d[2]>
<cfset sec=s_d[3]>
<cfset new_time = TimeFormat(createTime(hours, min, sec))>

Expected Output

start_time="05:00:00 PM"
duration="02:30:00"
then end_time should be 07:30:00 PM 

how to calculate end time like this?

Upvotes: 0

Views: 244

Answers (2)

Gavin Baumanis
Gavin Baumanis

Reputation: 403

<cfscript>

// Assuming HH:nn:ss format for ALL times

function durationToSeconds (string duration){
    if (listLen(arguments.duration, ":") EQ 3) {
        var HH2ss = listGetAt(arguments.duration, 1, ":") *60*60; // or 3600 if you prefer
        var nn2ss = listGetAt(arguments.duration, 2, ":") * 60;

        return HH2ss+nn2ss+listGetAt(arguments.duration, 3, ":");
    } else {
        return "Ensure that your times are in HH:nn:ss format";
    }
}

durationToSeconds("01:00:00"); // 3600
durationToSeconds("01:01:00"); // 3660
durationToSeconds("01:00:07"); // 3607
durationToSeconds("01:00"); // Ensure that your times are in HH:nn:ss format

startTime = "17:10:00";
duration = "02:20:30";
endTime = dateTimeFormat(dateAdd("s", durationToSeconds(duration), startTime), "HH:nn:ss"); //19:30:30

</cfscript>

Upvotes: 0

Dan Bracuk
Dan Bracuk

Reputation: 20804

Step 1 - Covert your duration to seconds.

Step 2 - Use ColdFusion's DateAdd function to calculate the end time.

Upvotes: 1

Related Questions