Skip to content

Instantly share code, notes, and snippets.

@CliffCrerar
Created October 23, 2019 00:21
Show Gist options
  • Save CliffCrerar/d8b567a4826ad41748a810430d3f0a42 to your computer and use it in GitHub Desktop.
Save CliffCrerar/d8b567a4826ad41748a810430d3f0a42 to your computer and use it in GitHub Desktop.
function changeDateFormat(dates) {
// Write the code that goes here
const regExp = /^\d{2,4}(\/|-)\d{2}(\/|-)\d{2,4}$/
const regExpG = /(-|\/)/
const newArr = [];
for (let i = 0; i < dates.length; i++) {
if(dates[i].match(regExp)===null){
break;
} else if (dates[i].match(regExp).length > 0) {
newArr.push(
fixFormat(
dates[i],
regExpG.exec(dates[i])[0],
regExpG.exec(dates[i]).index)
)
}
}
function fixFormat(date, splitter, firstPos) {
const spl = date.split(splitter);
if(splitter==='-' && firstPos===2){
return `${spl[2]}${spl[0]}${spl[1]}`
} else if (splitter==='/' && firstPos===2){
return `${spl[2]}${spl[1]}${spl[0]}`
} else if(splitter==='/' && firstPos===4){
return spl.join('');
}
}
return newArr
}
var dates = changeDateFormat(["2010/03/30", "15/12/2016", "11-15-2012", "20130720"]);
for (index = 0; index < dates.length; ++index) {
console.log(dates[index]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment