James T
James T

Reputation: 3320

Creating time from a string

I want to subtract time as to get a result of hour and minutes (not a date), for instance

02/26/2006 06:25 PM

minus

02/26/2006 06:23 PM

Into

2 Minutes

Also, the times I want to subtract from are strings, not datetime objects.

Upvotes: 1

Views: 1210

Answers (1)

Leigh
Leigh

Reputation: 28873

Convert the strings to date/time objects. Then you will be able to take advantage of date functions. Get the total difference in minutes and simple division / mod will give you the total hours and minutes.

<cfset time1   = parseDateTime("02/26/2006 06:25 PM")>
<cfset time2   = parseDateTime("02/26/2006 06:23 PM")>
<cfset diff    = dateDiff("n", time2, time1)>
<cfset hours   = int(diff / 60)>
<cfset minutes = diff mod 60>

Upvotes: 5

Related Questions