Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

javascript - Javascript-确保日期格式为YYYY-MM-DD(Javascript - Ensure Date is Formatted YYYY-MM-DD)

I am working with a mysql database that takes dates as YYYY-MM-DD.(我正在使用日期为YYYY-MM-DD的mysql数据库。)

I have an array named dates that contains so inputted values, I am trying to check to see if the inputted values are in the format YYYY-MM-DD and if they aren't then add them to the wrongdates array however i am completely lost on the logic that goes in the if statement to check if the format is correct(我有一个名为date的数组,其中包含如此输入的值,我试图检查输入的值是否为YYYY-MM-DD格式,如果没有,则将它们添加到错误的dates数组中,但是我完全迷失了在if语句中检查格式是否正确的逻辑上) var wrongdates = []; for(x=0;x<dates.length<x++){ if(.........){ wrongdates.push(dates[x].value); } } window.alert(wrongdates);   ask by Anna translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use regex to ensure the structure .(您可以使用regex来确保结构 。)

const dates = ['2019-99-99', 'bla-bal-bla', '2019-11-20']; const wrongdates = dates.filter(date => !/\d{4}\-[01]\d\-[0-3]\d/.test(date)); console.log(wrongdates);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...