如何在javascript中将DD-MMM-YYYY解析为YYYYMMDDHMMSS?

如何在javascript中将DD-MMM-YYYY解析为YYYYMMDDHMMSS?,javascript,Javascript,我想要一个JS函数,它采用以下格式作为输入: DD-MMM-YYYY 并返回此格式作为结果 YYYYMMDDHHMMSS 如何做到这一点?问题还在于接收日期中未提供时间部分,为什么不与日期函数一起使用 let date: Date = new Date(); let fullDateAndTime: string = date.getFullYear() + "" + (date.getMonth()+1) + "" + date.getHours() + "" + date.getMinu

我想要一个JS函数,它采用以下格式作为输入:

DD-MMM-YYYY
并返回此格式作为结果

YYYYMMDDHHMMSS

如何做到这一点?问题还在于接收日期中未提供时间部分,为什么不与日期函数一起使用

let date: Date = new Date();
let fullDateAndTime: string = date.getFullYear() + "" + (date.getMonth()+1) + "" + date.getHours() + "" + date.getMinutes() + "" + date.getMilliseconds();
document.write(fullDateAndTime + ""); // Print YYYYMMDDHHMMSS
因此,您可以使用simple if(少于10个月)进行检查:

let date:date=new date();
//date.getMonth()+1<10?“0”+date.getMonth()+1:date.getMonth()+1
//用三值if检查月份是否小于10
让fullDateAndTime:string=date.getFullYear()+“”+(date.getMonth()+1<10?+date.getMonth()+1:date.getMonth()+1+“”+date.getHours()+“”+date.getMinutes()+“”+date.get毫秒();
document.write(fullDateAndTime+“”);//打印YYYYMMDDHMMSS

它将按您的要求打印…

您尝试过什么吗?您唯一能做的就是假设一个时间。说,
00:00:00
或者你想要的任何东西。没有办法从一个日期中检索任何具体内容。谢谢,但如果我的osfar小于10,它将以M格式而不是MM格式返回月份!我很高兴能帮忙,祝你好运!如果你能接受社区的回答,我会非常高兴!
let date: Date = new Date();
// date.getMonth() + 1 < 10 ? "0" + date.getMonth() + 1 : date.getMonth() + 1
// Check if the month is less than 10 with ternary if
let fullDateAndTime: string = date.getFullYear() + "" + (date.getMonth() + 1 < 10 ? "0" + date.getMonth() + 1 : date.getMonth() + 1) + "" + date.getHours() + "" + date.getMinutes() + "" + date.getMilliseconds();
document.write(fullDateAndTime + ""); // Print YYYYMMDDHHMMSS