如何将我的瞬间js持续时间值限制为仅2位数?

时间:2017-10-26 08:29:04

标签: javascript momentjs

我正在使用moment.js来处理根据距离/速度计算的日期时间。我也在使用moment duration format plugin。当我将值传递给moment.duration然后格式化时,其中一些值超过2位,即

39:3452:98762

我希望这可以正确格式化为HH:mm:ss。这是我的代码。

var point1Time = 19.188019795;
var d1 = moment.duration(point1Time, 'hours');

var d1Hours = d1.format('HH');
var d1Mins = d1.format('mm', {forceLength: true });
var d1Secs = d1.format('ss', {forceLength: true });

正如您所看到的point1Time(我为此示例而输入的数字)是里程/速度,然后我将此值传递给moment作为{在duration中{1}}。之后,我尝试使用hours分隔HH:mm:ss。返回的值类似于formatd1Hours: 19d1Mins: 1151

我认为添加d1Secs: 69076会有助于将数字保持为两位,但它无法正常工作,因为我无法确定在何处定义该限制。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

您可以在format()方法中使用'HH:mm:ss'作为模板来获取所需的输出。

d1.format('mm')d1.format('ss')会提供asSecondsasMinutes的输出,而我认为您需要输出minutes()seconds()

这是一个实时样本:

var point1Time = 19.188019795;
var d1 = moment.duration(point1Time, 'hours');

console.log(d1.format('HH:mm:ss'));

console.log(d1.minutes(), d1.asMinutes());
console.log(d1.seconds(), d1.asSeconds());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>

答案 1 :(得分:0)

您可以将持续时间转换为秒,然后很容易找到您想要的格式

<span class="bold_text">Olympics</span>
<span class="greeting"></span>
<span class="badge ui-corner-all         colors         like_a logged_out"  onclick="return sr_login_popup();" id="login_placeholder" style="display:none;"                                    >LOGIN<span id="loginform_container"></span></span>
<span id="loginform_container"></span>
<span class="badge ui-corner-all         colors         like_a logged_in"   onclick="return sr_logout();"                                                                                      >LOGOUT</span>
<span class="badge ui-corner-all tooltip disabled_badge like_a ad_free"     onclick="document.location.href='/my/auth.cgi?do=ad_free_browsing'" tip="Surf Sports Reference Ad Free">AD FREE</span>
<span>&nbsp;</span>
<span class=bold_text>You Are Here</span>
<span itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a itemprop=url href="/olympics/"><span itemprop=title>OLY Home</span></a></span>
<span itemprop=title>OLY Home</span>
<span itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a itemprop=url href="/olympics/athletes/"><span itemprop=title>Athletes</span></a></span>
<span itemprop=title>Athletes</span>
<span itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><span class=bold_text><span itemprop=title>Frank Bizzoni</span></span></span>
<span class=bold_text><span itemprop=title>Frank Bizzoni</span></span>
<span itemprop=title>Frank Bizzoni</span>
<span class="bold_text">News:</span>
<span class="poptip" tip="We've just launched the first stage of our new Sports Reference Widget tool. By clicking the \"Embed\" tooltip just above any stats table on the site you can now get the code to add our daily updated stats to your site. See the widgets page for a description [...]"><a onclick="try { pageTracker._trackEvent('blog','click','area-yah'); } catch (err) {};"  href="http://widgets.sports-reference.com/">Sports-Reference.com Widgets - Our Stats, Your Site</a> <span class="small_text">&raquo;<a href="http://www.sports-reference.com/blog/2012/02/sports-reference-com-widgets-our-stats-your-site/" onclick="try { pageTracker._trackEvent('blog','click','area-yah'); } catch (err) {};" >post</a></span></span>
<span class="small_text">&raquo;<a href="http://www.sports-reference.com/blog/2012/02/sports-reference-com-widgets-our-stats-your-site/" onclick="try { pageTracker._trackEvent('blog','click','area-yah'); } catch (err) {};" >post</a></span>
<span class="bold_text">Full name:</span>
<span class="bold_text">Gender:</span>
<span class="bold_text">Height:</span>
<span class="bold_text">Born:</span>
<span id="necro-birth" data-birth="1875-05-07"><a href="/olympics/friv/birthdays.cgi?month=5&amp;day=7">May 7</a>, 1875</span>
<span class="bold_text">Died:</span>
<span id="necro-death" data-death="1926-12-25">December 25, 1926</span>
<span class="bold_text">Country:</span>
<span class="bold_text">Sport:</span>