简体   繁体   中英

Hide Hour and Minute if 0

I'm trying to remove the Hours and the minutes if they are 0

Example ( If hours = 0 )

0 H, 1m, 12s
to
1m, 12s

and( If minutes = 0)

0m, 12s
to
12s

But if the hours are

2H, 0m, 12s (display like this)
2H, 12s (not like this if the minute is 0)

This is what I've tried, but this is so wrong

<script>
    var countDownDate = new Date(<?php echo strtotime($getdateinfo)*1000; ?>).getTime();
    var x = setInterval(function() {
        var now = new Date().getTime();
        var distance = countDownDate - now;
        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var h = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var m = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var s = Math.floor((distance % (1000 * 60)) / 1000);

        if (h = 0);{
            document.getElementById("tts").innerHTML = m + "m " + s + "s ";
        }
        if (h = 0 & m = 0);{
            document.getElementById("tts").innerHTML = s + "s ";
        }
        else{
            document.getElementById("tts").innerHTML = h + " h "
    + m + "m " + s + "s ";
        }
    }, 1000);
</script>

Your code works, only your conditions where badly written:
if (h = 0 & m = 0);
You should use two or three equal sign to check for equality, two ampersands for the AND operator and remove the semicolon after the if condition.
if (h === 0 && m === 0)

Here is a working example:

 var date = "2019-11-15 00:12:12"; var countDownDate = new Date(date).getTime(); var getRemainingTime = function() { var now = new Date().getTime(); var distance = countDownDate - now; var days = Math.floor(distance / (1000 * 60 * 60 * 24)); var h = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var m = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var s = Math.floor((distance % (1000 * 60)) / 1000); if (h === 0 && m === 0) { document.getElementById("tts").innerHTML = s + "s "; } else if (h === 0) { document.getElementById("tts").innerHTML = m + "m " + s + "s "; } else { document.getElementById("tts").innerHTML = h + " h " + m + "m " + s + "s "; } }; var x = setInterval(getRemainingTime, 1000);
 <p id="tts"></p>

There's an easier way of doing that with directly using the javascript Date object:

 var date = new Date("2019-11-15 00:12:12"); // fix timezone differences date.setTime( date.getTime() + date.getTimezoneOffset()*60*1000 ); setInterval(function(){ var diff = new Date(date - new Date()); var d = diff.getDate(), h = diff.getHours(), m = diff.getMinutes(), s = diff.getSeconds(); document.querySelector("#ttl").innerHTML = [ d > 1? (d - 1) + "d": "", h? h + "h": (d > 1? "0h": ""), m? m + "m": ((d > 1 || h)? "0m": ""), s? s + "s": ((d > 1 || h || m)? "0s": ""), ].join(" "); }, 1000);
 <p id="ttl"></p>

Since this is a presentation problem, I would fix it in the presentation context (ie the string). Here is a quick example.

timeString.replace(/^0H, (0m, )?/, '')

This is obviously quick and dirty. You can cascade if statements to build the string, but if you're not calling this a crazy number of times, I find the regex solution easier to understand. Every second is fine.

Note: my answer uses commas because your examples do. But your code does not actually seem to put the commas, so you may want to remove them to make it work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM