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

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 == 1那么 num = ('0' + num);

182686 次浏览

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

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

为了使它成为一个字符串,那么检查长度(您应该检查 length === 1,而不是0)。

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

数字没有长度,但是你可以很容易地将数字转换成一个字符串,检查长度,如果有必要的话在前面加上0:

var strMonth = '' + date.getMinutes();
if (strMonth.length == 1) {
strMonth = '0' + strMonth;
}
var date = new Date("2012-01-18T16:03");


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

我假设您需要这个值作为字符串,您可以使用下面的代码。 它总是返回给你两位数分钟作为字符串。

const date_string = "2012-01-18T16:03";
const date = new Date(date_string);
let min = date.getMinutes();


if (min < 10) { // or min = min < 10 ? '0' + min : min;
min = '0' + min;
} else {
min = min + '';
}


console.log(min);

希望这个能帮上忙。

我通常使用这段代码:

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

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

我建议:

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

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

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

对 JS 来说很新鲜,所以这很有帮助 看这个问题的人也是最多的 这就是我如何在 div 中显示的“ class = “ min”

希望能帮到别人

另一个选择:

var dateTime = new Date();
var minutesTwoDigitsWithLeadingZero = ("0" + dateTime.getMinutes()).substr(-2);

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

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

如果你想用这个。

console.log(date.getFullMinutes());

哎呀,这些答案都不怎么样,连最上面的帖子都被提升了。开始吧,跨浏览器和更清晰的 int/string 转换。另外,我的建议是不要使用变量名“ date”来处理像 date = Date(...)这样严重依赖语言大小写敏感性的代码(这种方法很有效,但是当你使用不同语言、不同规则的服务器/浏览器代码时,这种方法就有风险了)。假设 javascript Date 在一个变量 current_date中:

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

这种技术是将“0”的最右边2个字符 (slice(-2))添加到 getMinutes()的字符串值上。因此:

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

还有

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

我没有看到任何 ES6的答案在这里,所以我将添加一个使用标准 JS 格式

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

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

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


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

使用两位数字分钟: new Date().toLocaleFormat("%M")

另一种简单的方法是在会议记录中加上一个前导零:

String(date.getMinutes()).padStart(2, "0");

意义: 使字符串长度为两个字符,如果缺少一个字符,则将 0设置在这个位置。

参见 PadStart (targetLlength,padString)的文档

如果您在项目中使用 AngularJS,只需注入 $filter 并像这样使用它:

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

您还可以格式化模板中的输出,更多关于过滤器 给你的信息。

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

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

另一个获得两位数分钟或小时的选项。

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


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

使用 ECMAScript 国际化 API,更多信息:

const date_string = "2012-01-18T16:03";
const date = new Date(date_string);
const twoDigitMinutes = date.toLocaleString("en-us", {year: 'numeric', month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit', second: "2-digit"});


console.log(twoDigitMinutes);

你可以使用 等一下 JS:

moment(date).format('mm')

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

希望能帮到别人

链接 github :
Https://github.com/moment/moment/

const date_string = "2012-01-18T16:03";
const date = new Date(date_string);
const dd = date.getDate();
const MM = date.getMonth();
const mm = date.getMinutes();
const HH = date.getHours();


// Year
const year = date.getFullYear();


// Month
const month = ("0" + (MM + 1)).slice(-2); // first month start from 0, so add +1


// Day
const day = ("0" + dd).slice(-2);


// Hour
const hour = ("0" + HH).slice(-2);


// Minutes
const minutes = ("0" + mm).slice(-2);


// Seconds
const seconds = ("0" + mm).slice(-2);


console.log(year, month, day, hour, minutes, seconds);