Javascript momentjs将单数字月转换为双数字月

Javascript momentjs将单数字月转换为双数字月,javascript,momentjs,Javascript,Momentjs,我正在尝试将一个数字月转换为两位数,例如,我从用户那里得到的00,转换为具有以下代码的数字 currentMonthNumeric: moment().format('M'), 我怎样才能用几个月的时间把一个(0)单位转换成两个(00)我想你有一个小的语法错误 const currentMonthNumeric=moment().格式('M') log(“currentMonthNumeric:+currentMonthNumeric”); //我相信你的代码用错了 console.log(

我正在尝试将一个数字月转换为两位数,例如,我从用户那里得到的00,转换为具有以下代码的数字

currentMonthNumeric: moment().format('M'),

我怎样才能用几个月的时间把一个(0)单位转换成两个(00)

我想你有一个小的语法错误

const currentMonthNumeric=moment().格式('M')
log(“currentMonthNumeric:+currentMonthNumeric”);
//我相信你的代码用错了
console.log(“错误代码:”+moment().month(currentMonthNumeric).format('MM'));//本月+1为双格式,例如08
//我相信正确的执行
console.log(“正确代码:+力矩(currentMonthNumeric,'MM')。格式('MM'));//本月采用双格式,例如07
//单月到双月示例
常数singleMonthStatic=“7”;
//错误代码
console.log(“错误代码静态:”+moment().month(singleMonthStatic).format('MM'));//08
//正确代码
console.log(“正确的代码静态:”+moment(singleMonthStatic,'M')。格式('MM'));//07
//如果你只是想从一开始就加倍
const currentMonthDoubleNumeric=moment().格式('MM')
log(“currentMonthDoubleNumeric:+currentMonthDoubleNumeric);//当月双倍,例如07

实际上,基址0将在七月返回
6
,而一月将是
0
。所以问题可能出在其他方面。可能是时区不同于您对特定输入集的预期,但不是所有输入。嗯。。这取决于currentMonthNumeric是什么,但您可以只+1…只需使用result@DanielTate
.add(1,'month)
或如何添加?@Nofel this.currentMonthNumeric+1您没有重现OP的问题,因为您不知道OP的
值是什么。他们说:where currentMonthNumeric:moment().format('M')True otw,但在OP的时区还是你的?一般来说,这个月不会改变,除非是在尖顶。此外,OP没有以源代码的形式显示带有此声明的代码。除非OP的意思是7月份的
06
,而不是
08
@GetSet,否则我添加了OP的代码,向您展示他们的实现是如何错误的。但是你是对的,我应该把它编辑成时区不可知的。因为在阅读文档时,
.format(“M”)
.format(“MM”)
将以1为基数返回月份。但是
.month()
方法接受以0为基数的参数。因此,本例中的格式返回
7
。但是,当传递到基本0调用,并通过随后对另一个调用“重新格式化”时,
7
变为
8