我想使用JsDatePick计算两个日期之间的天数差异 所以这是我的两个选择:
<script type="text/javascript">window.onload = function(){
new JsDatePick({ useMode:2, target:"field", cellColorScheme:"deepblue", dateFormat:"%Y-%m-%d" });
new JsDatePick({ useMode:2, target:"field2", cellColorScheme:"deepblue", dateFormat:"%Y-%m-%d" });};
</script>
所选日期保存在此文本框中:
<input type="text" id="field" name="dateDebut" placeholder="Date début" />
<input type="text" id="field2" name="dateFin" placeholder="Date Fin" />
我不知道如何计算这两天之间的差异日 ,有人请帮助我,我想在这个文本框上打印差异日的数量:
<input type="text" id="Difference" name=Difference" class="span3"/>
答案 0 :(得分:0)
在脚本标记内:
<script type="text/javascript">
window.onload = function(){
new JsDatePick({ useMode:2, target:"field", cellColorScheme:"deepblue", dateFormat:"%Y-%m-%d" });
new JsDatePick({ useMode:2, target:"field2", cellColorScheme:"deepblue", dateFormat:"%Y-%m-%d" });
};
function getValue(field){
console.log("getValue()"+document.getElementById(field).value);
return document.getElementById(field).value;
}
function setValue(field,value){
document.getElementById(field).value = value;
}
function showDifferenceInDays(field,field2,fieldOut){
var fieldDate = new Date(getValue(field));
var field2Date = new Date(getValue(field2));
var fieldMilliseconds = fieldDate.getTime();
//console.log(fieldMilliseconds);
var field2Milliseconds = field2Date.getTime();
//console.log(field2Milliseconds);
var diffMilliseconds = fieldMilliseconds-field2Milliseconds;
//console.log(diffMilliseconds);
var millisecondsInADay = 1000*60*60*24;
//console.log(diffMilliseconds/millisecondsInADay);
setValue(fieldOut,(diffMilliseconds/millisecondsInADay));
}
</script>
在正文标记内:
<input type="text" id="field" name="dateDebut" placeholder="Date début" />
<input type="text" id="field2" name="dateFin" placeholder="Date Fin" />
<input type="text" id="Difference" name=Difference" class="span3"/>
<input type="button" value="Show Difference" onClick="showDifferenceInDays('field','field2','Difference')" />
答案 1 :(得分:0)
function getDays(d1, d2) {
var time1 = d1.getTime();
var time2 = d2.getTime();
return parseInt((time2-time1)/(24*3600*1000));
}
date1 = new Date("6/04/2014")
//Wed Jun 04 2014 00:00:00 GMT+0530 (India Standard Time)
date2 = new Date("7/04/2014")
//Fri Jul 04 2014 00:00:00 GMT+0530 (India Standard Time)
getDays(date1,date2)
//output 30
答案 2 :(得分:0)
function getDays(date1, date2) {
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
return diffDays;
}
var date1 = new Date(document.getElementById('field').value);
var date2 = new Date(document.getElementById('field2').value);
getDays(date1,date2)
//output 10