Javascript 在几分钟内领先零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14529381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:06:59  来源:igfitidea点击:

Leading zeros in minutes

javascript

提问by charlie

I created a clock to be placed in the header of my website. The time is not displaying a zero for minutes < 10. For example if the time is 10:50, it will only show 10:5 ,I found a solution but unsure of how to implement it. Also if there is a better method please share.

我创建了一个时钟,放置在我网站的标题中。时间在分钟 < 10 内不显示零。例如,如果时间是 10:50,它只会显示 10:5 ,我找到了一个解决方案,但不确定如何实现它。另外如果有更好的方法请分享。

 var current;
window.onload = function () {

    current = new Date();

    document.getElementById("clock").innerHTML = current.getHours() + ":" +      current.getMinutes(); 

This is what I need

这就是我需要的

                if (minutes < 10)
                minutes = "0" + minutes

and this is the container for my clock

这是我的时钟的容器

    <span id="clock">&nbsp</span>

回答by Pete

You can just grab the first 5 characters of the time string.

您可以只获取时间字符串的前 5 个字符。

(new Date()).toTimeString().substr(0,5)

回答by Tomasz Nurkiewicz

And what is your issue?

你的问题是什么?

var minutes = (current.getMinutes() < 10? '0' : '') + current.getMinutes();

Since you'll have the same problem with hours, wrap it in a small utility function:

由于您会遇到与小时相同的问题,请将其包装在一个小的实用程序函数中:

function pad(var value) {
    if(value < 10) {
        return '0' + value;
    } else {
        return value;
    }
}

And later simply:

后来简单地说:

pad(current.getHours()) + ":" + pad(current.getMinutes())

回答by Plynx

Since you're likely to run into presentational issues in the future along the same lines, I'd recommend picking a favorite string formattingfunction for Javascript.

由于您将来可能会遇到同样的问题,我建议您为 Javascript选择一个最喜欢的字符串格式化函数。

Some examples:

一些例子:

Then you can do something like "{0:00}:{1:00}".format(current.getHours(), current.getMinutes())or even better,

然后你可以做类似"{0:00}:{1:00}".format(current.getHours(), current.getMinutes())甚至更好的事情,

var d = new Date();
var s = d.format("hh:mm:ss tt");
// Result: "02:28:06 PM"

回答by dmo

I like this way of doing things... Javascript add leading zeroes to date

我喜欢这种做事方式... Javascript 添加前导零迄今为止

const d = new Date();
const date = (`0${d.getMinutes()}`).slice(-2);
console.log(date); // 09;

2019 Update:But I now prefer

2019 年更新:但我现在更喜欢

const d = new Date();
const date = String(d.getMinutes()).padStart(2, '0');
console.log(date); // 09;

回答by Krtek

I tried this way. It's not brief but it works.

我试过这种方式。这不是简短的,但它的工作原理。

var dt = new Date();
if (parseInt(dt.getMinutes()) < 10) {minutes = "0" + dt.getMinutes();} else minutes = dt.getMinutes();
if (parseInt(dt.getSeconds()) < 10) {seconds = "0" + dt.getSeconds();} else seconds = dt.getSeconds();

var time = dt.getHours() + ":" + minutes + ":" + seconds;
document.write("Now is:" + time);

//Result: Now is: 22:47:00

I hope it will be useful

我希望它会很有用