English 中文(简体)
页: 1
原标题:Leading zeros in minutes
  • 时间:2013-01-25 19:54:05
  •  标签:
  • javascript

我制作了一个锁,放在我的网站上。 时间为零;10。 例如,如果时间为10:50,我只想表明10:5,我找到解决办法,但不知道如何执行。 如果有更好的方法,请分享。

 var current;
window.onload = function () {

    current = new Date();

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

这是我需要的。

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

这是我24小时的集装箱。

    <span id="clock">&nbsp</span>
最佳回答

由于你今后可能按照同样思路将问题列入列报,我建议对 Java稿使用一种优良的说明格式功能。

Some examples:

然后,你可以做如下事情:{0:00}:{}”format(当值.getHours(),当期。

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

你们只能 gr住时间的头5个特征。

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

你们的问题是什么?

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

由于你因时间关系而面临同样的问题,因此在小型公用事业功能中总结如下:

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

And later simply:

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

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

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

2019 Update: But I now prefer

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

我试图这样做。 它不是简短的,而是运作。

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

我希望这将是有益的。

Today I m using .toLocaleTimeString(), which by default returns something like "08:30:00" - based on the users browser locale. you can force a locale like this:

(new Date()).toLocaleTimeString("de-DE")

第2个选项参数更令人感兴趣:

(new Date()).toLocaleTimeString(
    "de-DE", 
    {
        hours: "2-digit",
        minutes: "2-digit",
        seconds: "2-digit",
    }
)

如果在备选办法中删除<代码>seconds,则只印制小时数和分钟。

So based on the original question, today I would use this:

var current;
window.onload = function () {
    current = new Date();
    document.getElementById("clock")
        .innerHTML = current.toLocaleTimeString(
            navigator.language,
            {
                hours: "2-digit",
                minute: "2-digit"
            }
        ); 

• 小型清洁/园:

var current;
var timeOptions = {
    hours: "2-digit",
    minute: "2-digit"
};
window.onload = function () {
    current = new Date();
    document
        .getElementById("clock")
        .innerHTML = current.toLocaleTimeString(navigator.language, timeOptions); 




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签