getMinutes()0-9-如何显示两位数字?


136
var date = "2012-01-18T16:03";
var date = new Date(date);

console.log(date.getMinutes());
console.log(date.getMinutes().length)

返回3。

  1. 如何使其返回“ 03”?
  2. 为什么.length退货未定?

我试过了,但是没有用:

如果那样的strlen == 1num = ('0' + num);


只是加起来,返回的.getMinutes()是一个整数,您不能.length从整数访问。为此,将数字解析为字符串,然后检查其长度(不建议在处理日期时使用)。例如:date.getMinutes().toString().length
ViniciusPires

Answers:


276
var date = new Date("2012-01-18T16:03");

console.log( (date.getMinutes()<10?'0':'') + date.getMinutes() );

2
<10?'0':''是什么意思?
user1063287

@ user1063287,这是一个内联if语句
Aryeh Armon

5
@ user1063287这意味着:“如果getMinutes()小于10,返回0,如果大于,返回一个空字符串。
迈克尔·乔瓦尼Pumo


(condition?true:false)在PHP中,您可以省略true语句(condition?:false),然后在JS中使用(condition || false)三元运算符en.wikipedia.org/wiki/Ternary_operation
llange

203

认为这些答案并不好,即使是最高职位也是如此。在这里,跨浏览器和更清洁的int / string转换。另外,我的建议是不要在代码中使用变量名“ date”,例如date = Date(...)您高度依赖语言区分大小写的代码(它可以工作,但是当您使用具有不同规则的不同语言的服务器/浏览器代码时会冒风险) 。因此,假设var中的javascript日期为current_date

mins = ('0'+current_date.getMinutes()).slice(-2);

该技术是(slice(-2))在字符串值之前添加最右边的2个字符“ 0” getMinutes()。所以:

"0"+"12" -> "012".slice(-2) -> "12"

"0"+"1" -> "01".slice(-2) -> "01"

7
优雅的解决方案
Oldenborg 2014年

1
slice(-2)的使用是违反直觉的,但是我喜欢它。
ChrisFox


18

优雅的ES6功能,可将日期格式化为hh:mm:ss

const leadingZero = (num) => `0${num}`.slice(-2);

const formatTime = (date) =>
  [date.getHours(), date.getMinutes(), date.getSeconds()]
  .map(leadingZero)
  .join(':');

12

如果可以的话,我想提供一个更简洁的解决方案。被接受的答案非常好。但是我会这样做的。

Date.prototype.getFullMinutes = function () {
   if (this.getMinutes() < 10) {
       return '0' + this.getMinutes();
   }
   return this.getMinutes();
};

现在,如果您想使用它。

console.log(date.getFullMinutes());

10

我建议:

var minutes = data.getMinutes();
minutes = minutes > 9 ? minutes : '0' + minutes;

它减少了一个函数的调用。考虑性能总是好的。它也很短;


4

您应该检查它是否小于10 ...不寻找它的长度,因为这是一个数字而不是字符串




3

.length未定义,因为getMinutes返回的是数字,而不是字符串。数字没有length属性。你可以做

var m = "" + date.getMinutes();

使其成为字符串,然后检查长度(您需要检查length === 1,而不是0)。


3

数字没有长度,但是您可以轻松地将数字转换为字符串,检查长度,然后在必要时添加0:

var strMonth =''+ date.getMinutes();
如果(strMonth.length == 1){
  strMonth ='0'+ strMonth;
}

3

我在这里看不到任何ES6答案,因此我将使用StandardJS格式添加一个答案

// ES6 String formatting example
const time = new Date()
const tempMinutes = new Date.getMinutes()
const minutes = (tempMinutes < 10) ? `0${tempMinutes}` : tempMinutes

3

我假设您需要将该值作为字符串。您可以使用下面的代码。它始终会返回给您两位两位数字的分钟作为字符串。

var date = new Date(date);
var min = date.getMinutes();

if (min < 10) {
min = '0' + min;
} else {
min = min + '';
}
console.log(min);

希望这可以帮助。


1

我通常使用这段代码:

var start = new Date(timestamp),
    startMinutes = start.getMinutes() < 10 ? '0' + start.getMinutes() : start.getMinutes();

它与@ogur接受的答案非常相似,但是在不需要0的情况下不会连接空字符串。不确定会更好。只是另一种方式!


1

您可以使用moment js:

moment(date).format('mm')

例如: moment('2019-10-29T21:08').format('mm') ==> 08

希望对别人有帮助


无论如何都非常好,谢谢:-)
Nadine

0
$(".min").append( (date.getMinutes()<10?'0':'') + date.getMinutes() );

JS的新功能,所以这对查看这个新功能的大多数人来说非常有帮助,所以这就是我如何将它显示在名为“ class =“ min”的div中

希望对别人有帮助


0

这个怎么样?这个对我有用!:)

var d = new Date();
var minutes = d.getMinutes().toString().replace(/^(\d)$/, '0$1');

0

另一种选择是获得两位数的分钟数或小时数。

var date = new Date("2012-01-18T16:03");

var minutes = date.toTimeString().slice(3, 5); 
var hours   = date.toTimeString().slice(0, 2); 


-2

如果您在项目中使用AngularJS,只需注入$ filter并按如下所示使用它:

$filter('date')(value, 'HH:mm')

您还可以在模板中格式化输出,更多信息请参见此处的过滤器。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.