Search code examples
javascriptregexcountdown

countdown.js other formatting


I'm using the very good script from http://countdownjs.org/ and pretty much all is working well. However, I want the format without the strings hours, minutes, seconds - just a plain timer (hh:mm:ss). I was thinking about a regex but this is very ineffective, I guess. So is there a better way? This is the code I have so far (which works, but in a very ineffective way):

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
}

Solution

  • Why putting it to a string instead of using the given object ? You would then be able to get the variables you need without doing some string replacements. For instance :

    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);