我有一个像这样的隐藏表格
HTML
<h2> First Date</h2>
<input type="hidden" class="datepicker" id="startDate" value="29-10-2015" />
<h2> Second Date</h2>
<input type="hidden" class="datepicker" id="endDate" value="29-12-2015" />
<button type="button" id="test">Click It</button>
<p>Total Weeks</p>
JAVASCRIPT
$("input").datepicker();
$('#test').click(function () {
var end = $('#endDate').datepicker({ dateFormat: 'dd-mm-yyyy'}).val();
var start = $('#startDate').datepicker({ dateFormat: 'dd-mm-yyyy'}).val();
var $weekDiff = Math.floor((end - start + 1) / (1000 * 60 * 60 * 24) / 7);
alert('Total Weeks :'+$weekDiff);
});
当我在数据库的隐藏输入字段中打印日期时,它采用我在此处粘贴的格式
答案 0 :(得分:4)
转换为ms并除以每周ms。
// date1 = $('#startDate').datepicker('getDate');
// date2 = $('#endDate').datepicker('getDate');
function nWeeks(date1, date2) {
var WEEK = 1000 * 60 * 60 * 24 * 7;
var date1ms = date1.getTime();
var date2ms = date2.getTime();
var diff = Math.abs(date2ms - date1ms);
return Math.floor(diff / WEEK);
}
Here是关于JS日期对象的参考。
答案 1 :(得分:2)
这是一种简单的方法:
$("input").datepicker();
$('#test').click(function() {
var end = $('#endDate').datepicker('getDate');
var start = $('#startDate').datepicker('getDate');
var $weekDiff = Math.floor((end - start + 1) / (1000 * 60 * 60 * 24) / 7);
$('#total').html($weekDiff);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
<h2> First Date</h2>
<input type="text" class="datepicker" id="startDate" value="12/10/2015" />
<h2> Second Date</h2>
<input type="text" class="datepicker" id="endDate" value="12/29/2015" />
<button type="button" id="test">Click It</button>
<p>Total Weeks</p><span id="total"></span>
&#13;
答案 2 :(得分:0)
您正在尝试将JS字符串转换为数学表达式中的整数,这将导致NaN
。
所以你需要先做Date.parse( string )
。
随着你的小提琴,它将是:
$("input").datepicker();
$('#test').click(function () {
var end = $('#endDate').datepicker({ dateFormat: 'dd-mm-yyyy'}).val();
var start = $('#startDate').datepicker({ dateFormat: 'dd-mm-yyyy'}).val();
end = Date.parse( end );
start = Date.parse( start );
alert( [end, start] );
var $weekDiff = Math.floor((end - start + 1) / (1000 * 60 * 60 * 24) / 7);
alert('Total Weeks :'+$weekDiff);
});
使用es6和一些缩短:
$("input").datepicker();
$('#test').click( () => {
const [end, start] = ["#endDate", "startDate"]
.map( e => Date.parse( e.datepicker({ dateFormat: 'dd-mm-yyyy'}).val() ) );
const diff = Math.floor((end - start + 1) / (1000 * 60 * 60 * 24) / 7);
console.log('Total Weeks :' + diff);
});
答案 3 :(得分:0)
你可以尝试这样的事情。
$("input").datepicker();
$('#test').click(function () {
var end = $('#endDate').datepicker({ dateFormat: 'dd-mm-yyyy'}).val();
var start = $('#startDate').datepicker({ dateFormat: 'dd-mm-yyyy'}).val();
var $weekDiff = Math.floor((end - start + 1) / (1000 * 60 * 60 * 24) / 7);
alert(daydiff(parseDate(start), parseDate(end)));
});
function parseDate(str) {
var mdy = str.split('/')
return new Date(mdy[2], mdy[0]-1, mdy[1]);
}
function daydiff(first, second) {
return Math.round((second-first)/(1000*60*60*24) / 7);
}
更新了您的jsfiddle
答案 4 :(得分:0)
它对我有用。
$('#test').click(function () {
var end = $('#endDate').datepicker({dateFormat: 'dd-mm-yyyy'}).val();
var start = $('#startDate').datepicker({dateFormat: 'dd-mm-yyyy'}).val();
var aDate1 = end.split('-');
var aDate2 = start.split('-');
end = Date.UTC(aDate1[2], aDate1[1] - 1, aDate1[0]);
start = Date.UTC(aDate2[2], aDate2[1] - 1, aDate2[0]);
alert(Math.floor((end - start) / (1000 * 60 * 60 * 24)));
});