[JavaScript]文字列yyyy-mm-ddからDateを得る
js
function getDateByYYYYMMDD(yyyymmdd) { var tmp = void 0, match = void 0; if (match = yyyymmdd.match(/[\d]{4}\-[\d]{2}\-[\d]{2}/)) { if (match.length) { tmp = match[0].split('-'); } } else if (match = yyyymmdd.match(/[\d]{4}[\d]{2}[\d]{2}/)) { if (match.length) { tmp = [match[0].substr(0, 4), match[0].substr(4, 2), match[0].substr(6, 2)]; } } if (!tmp || tmp.length < 3) { return null; } return new Date(Number(tmp[0]), Number(tmp[1]) - 1, Number(tmp[2]), 0, 0, 0, 0); }
es6
getDateByYYYYMMDD(yyyymmdd){ let tmp,match; if(match = yyyymmdd.match(/[\d]{4}\-[\d]{2}\-[\d]{2}/)){ if(match.length){ tmp = match[0].split('-'); } }else if(match = yyyymmdd.match(/[\d]{4}[\d]{2}[\d]{2}/)){ if(match.length){ tmp = [match[0].substr(0,4),match[0].substr(4,2),match[0].substr(6,2)]; } } if(!tmp || tmp.length < 3){ return null; } return new Date(Number(tmp[0]),Number(tmp[1])-1,Number(tmp[2]),0,0,0,0); }