Javascript 在javascript中以mm格式获取月份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2878184/
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
Get month in mm format in javascript
提问by Michael Kniskern
How do I retrieve the month from the current date in mmformat? (i.e. "05")
如何以mm格式从当前日期中检索月份?(即“05”)
This is my current code:
这是我当前的代码:
var currentDate = new Date();
var currentMonth = currentDate.getMonth() + 1;
回答by Gert Grenander
An alternative way:
另一种方式:
var currentMonth=('0'+(currentDate.getMonth()+1)).slice(-2)
回答by Matt
if (currentMonth < 10) { currentMonth = '0' + currentMonth; }
回答by Fernando Felix
One line solution:
一行解决方案:
var currentMonth = (currentDate.getMonth() < 10 ? '0' : '') + currentDate.getMonth();
回答by Benjie Junio
for the date:
日期:
("0" + this.getDate()).slice(-2)
and similar for the month:
本月类似:
("0" + (this.getMonth() + 1)).slice(-2)
回答by Simon Arnold
ES6 version, inpired by Gert Grenander
ES6 版本,灵感来自 Gert Grenander
let date = new Date();
let month = date.getMonth()+1;
month = `0${month}`.slice(-2);
回答by Igor Pavelek
If you do this
如果你这样做
var currentDate = new Date();
var currentMonth = currentDate.getMonth() + 1;
then currentMonth is a number, which you can format as you want, see this question that will help you with formatting: How can I format an integer to a specific length in javascript?
那么 currentMonth 是一个数字,您可以根据需要对其进行格式化,请参阅此问题以帮助您进行格式化:How can I format an integer to a specific length in javascript?
回答by Igor Pavelek
var CurrentDate = new Date();
CurrentDate.setMonth(CurrentDate.getMonth());
var day = CurrentDate.getDate();
var monthIndex = CurrentDate.getMonth()+1;
if(monthIndex<10){
monthIndex=('0'+monthIndex);
}
var year = CurrentDate.getFullYear();
alert(monthIndex);
回答by eyelidlessness
In order for the accepted answer to return a string consistently, it should be:
为了让接受的答案一致地返回一个字符串,它应该是:
if(currentMonth < 10) {
currentMonth = '0' + currentMonth;
} else {
currentMonth = '' + currentMonth;
}
Or:
或者:
currentMonth = (currentMonth < 10 ? '0' : '') + currentMonth;
Just for funsies, here's a version without a conditional:
只是为了好玩,这里有一个没有条件的版本:
currentMonth = ('0' + currentMonth).slice(-2);
Edit: switched to slice, per Gert G's answer, credit where credit is due; substrworks too, I didn't realize it accepts a negative startargument
编辑:切换到slice,根据 Gert G 的回答,信用到期;substr也有效,我没有意识到它接受否定start论点
回答by Hillsie
An alternative with ES6 template strings
ES6 模板字符串的替代方案
A solution for mm/yyyy. Not quite the question, but I guess remove the second part.
的解决方案mm/yyyy。不完全是问题,但我想删除第二部分。
const MonthYear = `${dateObj.getMonth() < 10 ? '0' : '' }${dateObj.getMonth()+1}/${dateObj.getFullYear()}`
const Month = `${dateObj.getMonth() < 10 ? '0' : '' }${dateObj.getMonth()+1}`

![Javascript 这可能是什么?[TsLint 错误:“必须适当处理 Promise”]](/res/img/loading.gif)