Display single digits with leading zero

3.9k Views Asked by At

I am trying to make a minutes dropdown. How do I make it come out as 01, 02, 03, 04, 05, 06, 07 08, 09, 10, 11, 12, etc.? Right now the first 9 numbers only come out as 1, 2, 3, 4, 5, 6, 7, 8, 9.

<select id="minutes">
  <cfloop from="1" to="60" index="m">
    <option>#m#</option>
  </cfloop>
</select>

Well, I understand why it's doing what it's doing and was expecting it anyway. Haha. Just wondered if there was a way of getting the 0s to come up without having to manually create 01-09 options.

2

There are 2 best solutions below

1
On BEST ANSWER

You can use numberFormat.

<select id="minutes">
    <cfoutput>
        <cfloop from="1" to="60" index="m">
            <option>#numberFormat(m,'00')#</option>
        </cfloop>
    </cfoutput>
</select>

See also this runnable example at trycf.com.

3
On

I can think of two options off the top of my head:

Option 1 - cfloop over timespan

Create a start and end time, and use cfloop with a step value of one minute (source: adobe docs )

<cfset startTime = CreateTime(0,0,0)>
<cfset endTime = CreateTime(0,59,59)>
<select id="minutes">
  <cfoutput>
    <cfloop from="#startTime#" to="#endtime#" index="m" step="#CreateTimeSpan(0,0,1,0)#">
      <option>#TimeFormat(m, 'mm')#</option>
    </cfloop>
  </cfoutput>
</select>

Option 2 - use RIGHT()

Just prepend 0 to all values, and take the right two characters:

<select id="minutes">
  <cfoutput>
    <cfloop from="0" to="59" index="m">
        <option>#Right(0 & m, 2)#</option>
    </cfloop>
  </cfoutput>
</select>