更新时间:2023-01-29 15:20:45
如果可以使用moment.js,则可以执行以下操作:
If you can use moment.js then the following does the job:
var dateFormats = {
"iso_int" : "YYYY-MM-DD",
"short_date" : "DD/MM/YYYY",
"iso_date_time": "YYYY-MM-DDTHH:MM:SS",
"iso_date_time_utc": "YYYY-MM-DDTHH:MM:SSZ"
//define other well known formats if you want
}
function getFormat(d){
for (var prop in dateFormats) {
if(moment(d, dateFormats[prop],true).isValid()){
return dateFormats[prop];
}
}
return null;
}
var dateInput = "2018-01-01T20:09:00";
var formatFound = getFormat(dateInput); //returns "YYYY-MM-DDTHH:MM:SS"
if(formatFound !==null){
//do stuff
}
检查默认情况下,请查看js文档以获取有关默认支持的dateFormats的更多信息,并使用它们填充您的dateFormats对象。
Check the moment js docs for more info on the supported dateFormats by default and populate your dateFormats object with them.