Javascript getMinutes() 0-9 - 如何显示两位数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8935414/
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
getMinutes() 0-9 - How to display two digit numbers?
提问by Mark Fondy
var date = "2012-01-18T16:03";
var date = new Date(date);
console.log(date.getMinutes());
console.log(date.getMinutes().length)
This returns 3.
这将返回 3。
- How do I make it return '03'?
- Why does
.length
return undefinded?
- 我如何让它返回'03'?
- 为什么
.length
返回未定义?
I tried this, but it did not work:
我试过这个,但没有用:
If strlen == 1
then num = ('0' + num);
如果strlen == 1
那么num = ('0' + num);
回答by ogur
var date = new Date("2012-01-18T16:03");
console.log( (date.getMinutes()<10?'0':'') + date.getMinutes() );
回答by Bambam
Yikes these answers aren't great, even the top post upticked. Here y'go, cross-browser and cleaner int/string conversion. Plus my advice is don't use a variable name 'date' with code like date = Date(...)
where you're relying heavily on language case sensitivity (it works, but risky when you're working with server/browser code in different languages with different rules). So assuming the javascript Date in a var current_date
:
哎呀,这些答案都不是很好,即使是最高职位也有所上升。在这里,跨浏览器和更清晰的 int/string 转换。另外,我的建议是不要在代码中使用变量名称“日期”,例如date = Date(...)
您严重依赖语言区分大小写的地方(它可以工作,但是当您使用具有不同规则的不同语言的服务器/浏览器代码时有风险) . 因此,假设 var 中的 javascript 日期current_date
:
mins = ('0'+current_date.getMinutes()).slice(-2);
The technique is take the rightmost 2 characters (slice(-2))
of "0" prepended onto the string value of getMinutes()
. So:
该技术是将最右边的 2 个字符(slice(-2))
“0”附加到 的字符串值上getMinutes()
。所以:
"0"+"12" -> "012".slice(-2) -> "12"
and
和
"0"+"1" -> "01".slice(-2) -> "01"
回答by Kai Noack
Another short way is to fill the minutes with a leading zero using:
另一种简短的方法是使用前导零填充分钟:
String(date.getMinutes()).padStart(2, "0");
Meaning: Make the string two chars long, if a char is missing then set 0
at this position.
含义:使字符串长度为两个字符,如果缺少字符,则0
在此位置设置。
See docs at str.padStart(targetLength, padString)
回答by sf77
Elegant ES6 function to format a date into hh:mm:ss
:
优雅的 ES6 函数将日期格式化为hh:mm:ss
:
const leadingZero = (num) => `0${num}`.slice(-2);
const formatTime = (date) =>
[date.getHours(), date.getMinutes(), date.getSeconds()]
.map(leadingZero)
.join(':');
回答by KapteinMarshall
I would like to provide a more neat solution to the problem if I may.The accepted answer is very good. But I would have done it like this.
如果可以的话,我想为这个问题提供一个更简洁的解决方案。接受的答案非常好。但我会这样做。
Date.prototype.getFullMinutes = function () {
if (this.getMinutes() < 10) {
return '0' + this.getMinutes();
}
return this.getMinutes();
};
Now if you want to use this.
现在如果你想使用这个。
console.log(date.getFullMinutes());
回答by user2846569
I suggest:
我建议:
var minutes = data.getMinutes();
minutes = minutes > 9 ? minutes : '0' + minutes;
it is one function call fewer. It is always good to think about performance. It is short as well;
少了一个函数调用。考虑性能总是好的。它也很短;
回答by Yaron U.
you should check if it is less than 10... not looking for the length of it , because this is a number and not a string
你应该检查它是否小于 10...不是在寻找它的长度,因为这是一个数字而不是一个字符串
回答by IanGSY
Another option:
另外一个选项:
var dateTime = new Date();
var minutesTwoDigitsWithLeadingZero = ("0" + dateTime.getMinutes()).substr(-2);
回答by Jordan Papaleo
I dont see any ES6 answers on here so I will add one using StandardJS formatting
我在这里没有看到任何 ES6 答案,所以我将使用 StandardJS 格式添加一个
// ES6 String formatting example
const time = new Date()
const tempMinutes = new Date.getMinutes()
const minutes = (tempMinutes < 10) ? `0${tempMinutes}` : tempMinutes
回答by Glenn
Numbers don't have a length, but you can easily convert the number to a string, check the length and then prepend the 0 if it's necessary:
数字没有长度,但您可以轻松地将数字转换为字符串,检查长度,然后在必要时添加 0:
var strMonth = '' + date.getMinutes(); if (strMonth.length == 1) { strMonth = '0' + strMonth; }