如何以 2 位格式获取 JavaScript 的月份和日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6040515/
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
How do I get Month and Date of JavaScript in 2 digit format?
提问by srini
When we call getMonth()
and getDate()
on date
object, we will get the single digit number
.
For example :
当我们在对象上调用getMonth()
andgetDate()
时date
,我们会得到single digit number
. 例如 :
For january
, it displays 1
, but I need to display it as 01
. How to do that?
对于january
,它显示1
,但我需要将其显示为01
. 怎么做?
回答by Hugo
("0" + this.getDate()).slice(-2)
for the date, and similar:
对于日期,以及类似的:
("0" + (this.getMonth() + 1)).slice(-2)
for the month.
本月。
回答by Qiniso
If you want a format like "YYYY-MM-DDTHH:mm:ss", then this might be quicker:
如果你想要像“YYYY-MM-DDTHH:mm:ss”这样的格式,那么这可能会更快:
var date = new Date().toISOString().substr(0, 19);
// toISOString() will give you YYYY-MM-DDTHH:mm:ss.sssZ
Or the commonly used MySQL datetime format "YYYY-MM-DD HH:mm:ss":
或者常用的MySQL日期时间格式“YYYY-MM-DD HH:mm:ss”:
var date2 = new Date().toISOString().substr(0, 19).replace('T', ' ');
I hope this helps
我希望这有帮助
回答by Sergey Metlov
Example for month:
月份示例:
function getMonth(date) {
var month = date.getMonth() + 1;
return month < 10 ? '0' + month : '' + month; // ('' + month) for string result
}
You can also extend Date
object with such function:
您还可以Date
使用此类功能扩展对象:
Date.prototype.getMonthFormatted = function() {
var month = this.getMonth() + 1;
return month < 10 ? '0' + month : '' + month; // ('' + month) for string result
}
回答by Marcel
The best way to do this is to create your own simple formatter (as below):
最好的方法是创建自己的简单格式化程序(如下所示):
getDate()
returns the day of the month (from 1-31)getMonth()
returns the month (from 0-11) < zero-based, 0=January, 11=DecembergetFullYear()
returns the year (four digits) < don't use getYear()
getDate()
返回月份中的第几天(从 1-31)getMonth()
返回月份(从 0-11) <从零开始,0=一月,11=十二月getFullYear()
返回年份(四位数字)<不要使用getYear()
function formatDateToString(date){
// 01, 02, 03, ... 29, 30, 31
var dd = (date.getDate() < 10 ? '0' : '') + date.getDate();
// 01, 02, 03, ... 10, 11, 12
var MM = ((date.getMonth() + 1) < 10 ? '0' : '') + (date.getMonth() + 1);
// 1970, 1971, ... 2015, 2016, ...
var yyyy = date.getFullYear();
// create the format you want
return (dd + "-" + MM + "-" + yyyy);
}
回答by SomeGuyOnAComputer
Why not use padStart
?
为什么不使用padStart
?
var dt = new Date();
year = dt.getYear() + 1900;
month = (dt.getMonth() + 1).toString().padStart(2, "0");
day = dt.getDate().toString().padStart(2, "0");
console.log(year + '/' + month + '/' + day);
This will always return 2 digit numbers even if the month or day is less than 10.
即使月或日小于 10,这也将始终返回 2 位数字。
Notes:
笔记:
- This will only work with Internet Explorer if the js code is transpiled using babel.
getYear()
returns the year from 1900 and doesn't requirepadStart
.getMonth()
returns the month from 0 to 11.- 1 is added to the month before padding to keep it 1 to 12
getDate()
returns the day from 1 to 31.- the 7th day will return
07
and so we do not need to add 1 before padding the string.
- the 7th day will return
- 如果使用babel转译 js 代码,这仅适用于 Internet Explorer 。
getYear()
返回 1900 年的年份,不需要padStart
.getMonth()
返回从 0 到 11 的月份。- 在填充前将 1 添加到月份以保持 1 到 12
getDate()
返回从 1 到 31 的日期。- 第 7 天将返回
07
,因此我们不需要在填充字符串之前加 1。
- 第 7 天将返回
回答by Gnanasekaran Ebinezar
The following is used to convert db2 date format i.e YYYY-MM-DD using ternary operator
以下用于使用三元运算符转换db2日期格式即YYYY-MM-DD
var currentDate = new Date();
var twoDigitMonth=((currentDate.getMonth()+1)>=10)? (currentDate.getMonth()+1) : '0' + (currentDate.getMonth()+1);
var twoDigitDate=((currentDate.getDate())>=10)? (currentDate.getDate()) : '0' + (currentDate.getDate());
var createdDateTo = currentDate.getFullYear() + "-" + twoDigitMonth + "-" + twoDigitDate;
alert(createdDateTo);
回答by ssamuel68
function monthFormated(date) {
//If date is not passed, get current date
if(!date)
date = new Date();
month = date.getMonth();
// if month 2 digits (9+1 = 10) don't add 0 in front
return month < 9 ? "0" + (month+1) : month+1;
}
回答by Fernando Vezzali
I would do this:
我会这样做:
var d = new Date('January 13, 2000');
var s = d.toLocaleDateString('en-US', { month: '2-digit', day: '2-digit', year: 'numeric' });
console.log(s); // prints 01/13/2000
回答by Andrés
Just another example, almost one liner.
再举一个例子,几乎是一个班轮。
var date = new Date();
console.log( (date.getMonth() < 9 ? '0': '') + (date.getMonth()+1) );
回答by Crazy Barney
If it might spare some time I was looking to get:
如果可以腾出一些时间,我希望得到:
YYYYMMDD
for today, and got along with:
今天,和相处:
const dateDocumentID = new Date()
.toISOString()
.substr(0, 10)
.replace(/-/g, '');