如何以2位数格式获取JavaScript的月份和日期? [英] How do I get Month and Date of JavaScript in 2 digit format?
本文介绍了如何以2位数格式获取JavaScript的月份和日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我们在日期$上调用
getMonth()
和 getDate()
时c $ c> object,我们将获得单位数
。
例如:
When we call getMonth()
and getDate()
on date
object, we will get the single digit number
.
For example :
对于 january
,它显示 1
,但我需要将其显示为 01
。怎么做?
For january
, it displays 1
, but I need to display it as 01
. How to do that?
推荐答案
("0" + this.getDate()).slice(-2)
的日期,类似:
("0" + (this.getMonth() + 1)).slice(-2)
当月。
这篇关于如何以2位数格式获取JavaScript的月份和日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文