Skip to content

Instantly share code, notes, and snippets.

@isummation
Created October 18, 2013 04:10
Show Gist options
  • Save isummation/7036381 to your computer and use it in GitHub Desktop.
Save isummation/7036381 to your computer and use it in GitHub Desktop.
ColdFusion function for returning time slots
<cfdump var="#TimeSlot(MinuteSlot = 15, CreatedDate = now())#" />
<cfdump var="#TimeSlot(MinuteSlot = 15)#" />
<cffunction name="TimeSlot" output="false" access="public" returntype="any">
<cfargument name="MinuteSlot" type="numeric" required="true">
<cfargument name="CreatedDate" required="false" hint="If it is passed then function will return the time slot of the createdDate">
<cfif 1440 MOD arguments.MinuteSlot NEQ 0>
<cfthrow type="user" message="Invalid Minute slot">
</cfif>
<cfset var i = 0>
<cfset var retArray = arrayNew(1)>
<cfset var TotalSlots = 1440 / arguments.MinuteSlot>
<cfset var StartTime = CreateDateTime(2013, 1, 1, 0, 0, 0)>
<cfset var EndTime = dateAdd("n", arguments.MinuteSlot, StartTime)>
<cfset var ArgsCreatedDate = "">
<cfloop from="1" to="#TotalSlots#" index="i">
<cfset retArray[i] = timeFormat(StartTime, "hh:mm TT") & " To " & timeFormat(EndTime, "hh:mm TT")>
<cfif structKeyExists(arguments, "CreatedDate") AND isDate(arguments.CreatedDate)>
<cfset ArgsCreatedDate = createDateTime(2013, 1, 1, Hour(arguments.CreatedDate), Minute(arguments.CreatedDate), Second(arguments.CreatedDate))>
<cfif ArgsCreatedDate GTE StartTime AND ArgsCreatedDate LTE EndTime>
<cfreturn retArray[i]>
</cfif>
</cfif>
<cfset StartTime = EndTime>
<cfset EndTime = dateAdd("n", arguments.MinuteSlot, StartTime)>
</cfloop>
<cfreturn retArray>
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment