Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取javascript中mm格式的月份_Javascript_Date - Fatal编程技术网

获取javascript中mm格式的月份

获取javascript中mm格式的月份,javascript,date,Javascript,Date,如何以mm格式从当前日期检索月份?(即“05”) 这是我当前的代码: var currentDate = new Date(); var currentMonth = currentDate.getMonth() + 1; if(currentmount

如何以
mm
格式从当前日期检索月份?(即“05”)

这是我当前的代码:

var currentDate = new Date();
var currentMonth = currentDate.getMonth() + 1;
if(currentmount<10){currentmount='0'+currentmount;}
另一种方法:

var currentMonth=('0'+(currentDate.getMonth()+1)).slice(-2)

为了使接受的答案一致地返回字符串,它应该是:

if(currentMonth < 10) {
    currentMonth = '0' + currentMonth;
} else {
    currentMonth = '' + currentMonth;
}
编辑:根据Gert G的回答,切换到
切片
,信用到期
substr
也可以工作,我不知道如果这样做,它会接受一个否定的
start
参数

var currentDate = new Date();
var currentMonth = currentDate.getMonth() + 1;
那么currentMonth是一个数字,您可以根据需要对其进行格式设置。请参阅此问题,它将帮助您进行格式设置:

有关日期:

("0" + this.getDate()).slice(-2)
本月的情况与此类似:

("0" + (this.getMonth() + 1)).slice(-2)
单线解决方案:

var currentMonth = (currentDate.getMonth() < 10 ? '0' : '') + currentDate.getMonth();
var currentMonth=(currentDate.getMonth()<10?'0':'')+currentDate.getMonth();
var CurrentDate=新日期();
CurrentDate.setMonth(CurrentDate.getMonth());
var day=CurrentDate.getDate();
var monthIndex=CurrentDate.getMonth()+1;

如果(monthIndexES6版本,由Gert Grenander输入

let date = new Date();
let month = date.getMonth()+1;
month = `0${month}`.slice(-2);

使用ES6模板字符串的替代方案

mm/yyyy
的解决方案。问题不太清楚,但我想删除第二部分

const MonthYear=`${dateObj.getMonth()<10?'0':''}${dateObj.getMonth()+1}/${dateObj.getFullYear()}`
const Month=`${dateObj.getMonth()<10?'0':'}${dateObj.getMonth()+1}`

此处的答案引用了一个,但没有代码示例。这是一个自ES2017以来简化的一般字符串填充问题。您可以使用和确保字符串具有固定的最小长度

返回的是从0(1月)到11(12月)的数字。要表示为从“01”到“12”的字符串,可以执行以下操作:

let date=新日期()
let month=date.getMonth()+1
let display=month.toString().padStart(2,'0')
console.log(display)//01、02、…、“09”、“10”、“11”、“12”中的一个
这同样适用于从1到31的数字的返回:

console.log(
新建日期().getDate().toString().padStart(2,'0')
)//01、02、…、“31”中的一个
或者简单地说,任何希望成为最小长度字符串的值:

let greeting='world'.padStart(17,'hello')
console.log(问候语)/“hello world”

谢谢!我原来有
if(currentmount<9){currentmount=“0”+currentmount;}
但它不起作用。我想我需要单引号而不是双引号。奇数..引号的类型应该不重要!可能是类型强制的工件和
+
运算符..您需要
<10
否则9将不会返回'09'@Matt:当然,这是一件小事,但现在如果是11月或12月,您有一个int,在所有其他情况下都有一个字符串。这可能会在进一步连接时导致一些意外行为(例如,如果另一个连接的值是数字)。我会在
else
中使用
toString
@Matt:是的,这就是为什么我建议您在else中使用
toString
,因此10、11和12(不敢相信我在上次评论中错过了10月)也是字符串,即使它们没有前导0。Gert G的解决方案是实现相同效果的另一种方法。
("0" + (this.getMonth() + 1)).slice(-2)
var currentMonth = (currentDate.getMonth() < 10 ? '0' : '') + currentDate.getMonth();
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);
let date = new Date();
let month = date.getMonth()+1;
month = `0${month}`.slice(-2);