<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<script>
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() +1;
var day = date.getDay();
//日付を0字詰めにする
function zeroFormat(num){
if(num < 10){
return '0' + String(num);
} else {
return num;
}
}
var year = zeroFormat(year);
var month = zeroFormat(month);
var day = zeroFormat(day);
var triggerDate = year + month + day;
//セパレート処理をする
function dateSeparate(targetDate){
var date = targetDate;
if(parseInt(date.slice(0,4),10) < 2000 ){
var len = 2;
} else {
var len = date.length;
}
switch(len){
case 8:
return date.slice(0,4) + '/' + date.slice(4,6) + '/' + date.slice(6,8);
break
case 4:
return date.slice(4,6) + '/' + date.slice(6,8);
break
case 2:
return date.slice(4,6);
break
default:
return false;
break
}
}
console.log(dateSeparate(triggerDate));
</script>
</body>
</html>