亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在倒計時中將時間從秒格式化為 HH:MM:SS

在倒計時中將時間從秒格式化為 HH:MM:SS

Helenr 2022-06-05 16:41:07
我有這個簡單的倒計時:function offer_countdown_timer(countdown_start, countdown_time, update, complete) {  var start = new Date(countdown_start).getTime();  var interval = setInterval(function() {    var now = countdown_time-(new Date().getTime()-start);    if( now <= 0) {      clearInterval(interval);      complete();    } else {      update(Math.floor(now/1000));    }  },100); // the smaller this number, the more accurate the timer will be}在這里我稱之為:<script>    offer_countdown_timer(      '<%= s.created_at%>',      3600000, // 1 hour in milliseconds      function(timeleft) { // called every step to update the visible countdown        var txt = timeleft+' seconds';        $('#tender_countdown_<%= s.id %>').html(txt);        //$('#tender_countdown_<%= s.id %>').html(moment(txt).format('HH:mm:ss'));                },      function() {        $('#product_<%= s.id %>').html('Offer has expired!');      }    );</script>這個的輸出是:773 seconds(它正在倒計時)我想看到這樣的東西(HH:ss:mm):00:12:53(并倒數)。我嘗試使用它(使用 Moment.js lib - https://momentjs.com/docs/):$('#tender_countdown_<%= s.id %>').html(moment(txt).format('HH:mm:ss'));    但在這種情況下,輸出是這樣的:01:00:00時間信息有誤,不倒計時。這是為什么?如何正確格式化倒計時時間?
查看完整描述

1 回答

?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

秒數是抽象的持續時間,而不是代表特定時刻的日期。Moment 的構造函數希望您給它一個日期字符串。


Moment 有一個 Duration 對象,可以解析您的數據。它還沒有很好的格式化功能,但你可以很容易地構建所需的輸出:


var txt = 773;

var m = moment.duration(txt, "s");

var output = m.hours() + ":" + m.minutes() + ":" + m.seconds();

console.log(output);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>



查看完整回答
反對 回復 2022-06-05
  • 1 回答
  • 0 關注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號