How do I use .toLocaleTimeString() without displaying seconds?

前端 未结 11 963
小蘑菇
小蘑菇 2020-12-07 15:56

I\'m currently attempting to display the user\'s time without displaying the seconds. Is there a way I can do this using Javascript\'s .toLocaleTimeString()?

Doing s

11条回答
  •  [愿得一人]
    2020-12-07 16:40

    I think the original question can easily be answered with something being overlooked so far: a simple string split. The time being returned is a text string, just split it on the ":" delimiter and reassemble the string the way you want. No plug ins, no complicated scripting. and here ya go:

    var myVar=setInterval(function(){myTimer()},1000);
    
    function myTimer() {
        var d = new Date();
        currentNow = d.toLocaleTimeString();
        nowArray = currentNow.split(':');
        filteredNow = nowArray[0]+':'+nowArray[1];
        document.getElementById("demo").innerHTML = filteredNow;
    }
    

提交回复
热议问题