JavaScript入門–日付の差分日数を求める方法

JavaScriptコード:
<script language="JavaScript">
function getDate(strDate){
if(strDate==null||strDate===undefined) return null;
var date = new Date();
try{
if(strDate == undefined){
date= null;
}else if(typeof strDate == 'string’){
strDate = strDate.replace(/:/g,’-');
strDate = strDate.replace(/ /g,’-');
var dtArr = strDate.split(“-“);
if(dtArr.length>=3&&dtArr.length<6){
date=new Date(dtArr[0], dtArr[1], dtArr[2]);
}else if(date.length>8){
date=new Date(Date.UTC(dtArr[0],dtArr[1]-1,dtArr[2],dtArr[3]-8,dtArr[4],dtArr[5]));
}
}else{
date = null;
}
return date;
}catch(e){
alert('日付フォーマットが間違えた:’ + e.message);
}
}
function subTimeCalu(){
var time1 = “2016-06-12";
var time2 = “2016-12-10";
var timeslong = getDate(time1).getTime()-getDate(time2).getTime();
alert(timeslong/(1000*60*60*24))
}
subTimeCalu();
</script>

JavaScript

Posted by arkgame