显示带前导零的单个数字

Display single digits with leading zero

我正在尝试制作分钟下拉列表。我如何让它以 01, 02, 03, 04, 05, 06, 07 08, 09, 10, 11, 12 等形式出现?现在前 9 个数字只显示为 1, 2, 3, 4, 5, 6, 7, 8, 9.

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

好吧,我理解它为什么会做它正在做的事情并且无论如何都期待它。哈哈。只是想知道是否有一种方法可以让 0 出现而无需手动创建 01-09 选项。

我能想到两个选项:

选项 1 - cfloop over timespan

创建起止时间,并使用步长为一分钟的cfloop(来源: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>

选项 2 - 使用 RIGHT()

只需在所有值前加上 0,并取正确的两个字符:

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

您可以使用 numberFormat

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

另请参阅此 runnable example at trycf.com