Javascript 日期:如有必要,确保 getMinutes()、getHours()、getSeconds() 将 0 放在前面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3313875/
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
Javascript Date: Ensure getMinutes(), getHours(), getSeconds() puts 0 in front if necessary
提问by MegaMatt
Looking for a creative way to be sure values that come from the getHours, getMinutes, and getSeconds() method for the javascript Date object return "06"instead of 6(for example). Are there any parameters that I don't know about? Obviously I could write a function that does it by checking the length and prepending a "0"if need be, but I thought there might be something more streamlined than that.
寻找一种创造性的方法来确保来自 javascript Date 对象返回的 getHours、getMinutes 和 getSeconds() 方法的值"06"而不是6(例如)。有什么我不知道的参数吗?显然,我可以编写一个函数,通过检查长度并"0"在需要时添加一个来完成它,但我认为可能有比这更简化的东西。
Thanks.
谢谢。
回答by Andy E
Similar to @jdmichal's solution, posting because I'd prefer something a little shorter:
类似于@jdmichal 的解决方案,发布是因为我更喜欢更短的内容:
function pad(n) { return ("0" + n).slice(-2); }
pad(6); // -> "06"
pad(12); // -> "12"
Rather than add individual methods to Date.prototype, you could just add this method to Number.prototype:
而不是单个的方法添加到Date.prototype,你可以只添加这个方法Number.prototype:
Number.prototype.pad = function (len) {
return (new Array(len+1).join("0") + this).slice(-len);
}
// Now .pad() is callable on any number, including those returned by
var time = date.getHours().pad(2) + ":"
+ date.getMinutes().pad(2) + ":"
+ date.getSeconds().pad(2);
回答by jdmichal
Update: ECMAScript 2017 (ECMA-262)
更新:ECMAScript 2017 (ECMA-262)
padStarthas been added to pad the beginning of a string with another string, where the first value is the length it should be and the second value being what to pad it with.
已添加padStart以用另一个字符串填充字符串的开头,其中第一个值是它应该是的长度,第二个值是用什么来填充它。
For example:
例如:
let d = new Date()
let h = `${d.getHours()}`.padStart(2, '0')
let m = `${d.getMinutes()}`.padStart(2, '0')
let s = `${d.getSeconds()}`.padStart(2, '0')
let displayDate = h + ":" + m + ":" + s
// Possible Output: 09:01:34
Pre-ECMAScript 2017
ECMAScript 2017 之前的版本
As far as I know, there's not. And I do this all the time for converting dates to the XML dateTime format.
据我所知,没有。我一直这样做是为了将日期转换为 XML dateTime 格式。
It's also important to note that those methods you list return a number, not a string.
同样重要的是要注意您列出的那些方法返回一个数字,而不是一个字符串。
You can, of course, add these yourself by modifying Date.prototype.
当然,您可以通过修改Date.prototype.
Date.prototype.getHoursTwoDigits = function()
{
var retval = this.getHours();
if (retval < 10)
{
return ("0" + retval.toString());
}
else
{
return retval.toString();
}
}
var date = new Date();
date.getHoursTwoDigits();
回答by Nilesh
function pad2(number) {
return (number < 10 ? '0' : '') + number
}
document.write(pad2(2) + '<br />');
document.write(pad2(10) + '<br />');
OUTPUT:
输出:
02
02
10
10

