[英]String date to format in another type of format JavaScript
I need a way to parse string date to another format string or date我需要一种方法将字符串日期解析为另一种格式字符串或日期
Tuesday, December 28, 2022 this string date into that format 2022-12-28
I can make it with hardcoding but I think it is not the best way, any good ideas on how I can format that string?我可以用硬编码来实现,但我认为这不是最好的方法,关于如何格式化该字符串有什么好的想法吗?
The way to parse string date to another format string or date like this 2022-12-28 is:将字符串日期解析为另一种格式字符串或日期(如 2022-12-28)的方法是:
function dateFormat(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
console.log(dateFormat('Sun Dec 12,2022'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.