.js其他格式

countdown.js other formatting

本文关键字:格式 其他 js      更新时间:2023-09-26

我使用非常好的脚本从http://countdownjs.org/和几乎所有的工作都很好。然而,我想要的格式没有字符串小时,分钟,秒 -只是一个普通的计时器(hh:mm:ss)。我正在考虑一个正则表达式,但这是非常无效的,我猜。那么有没有更好的办法呢?这是我到目前为止的代码(它可以工作,但以一种非常无效的方式):

var now = new Date();
var totalEnd = now.setHours(now.getHours() + 8);
var timeString = countdown( null, totalEnd, countdown.HOURS|countdown.MINUTES|countdown.SECONDS ).toString();
if (timeString) {
    timeString = timeString.replace(/'s?hours?,|'s?minutes?,/gi, ':');
    timeString = timeString.replace(/'s|and|seconds?/gi, '');
    // now I have a timestring with hh:mm:ss
    // but I want to avoid the regex
}

为什么把它放在一个字符串而不是使用给定的对象?这样,您就可以在不进行字符串替换的情况下获得所需的变量。例如:

var now = new Date();
var totalEnd = now.setHours(now.getHours() + 8);
var time = countdown( null, totalEnd, countdown.HOURS|countdown.MINUTES|countdown.SECONDS );
alert(time.days);