Można użyć datepicker natywną getDate funkcji, aby uzyskać obiekt daty. Następnie korzystając z informacji z this SO answer w odniesieniu do biorąc pod uwagę oszczędności czasu letniego pod rachunkiem możesz użyć czasów dat UTC, aby uzyskać dokładne różnice.
Oprócz dokonania ustawiania czasu datę niezwykle proste, jeśli można zmienić value
swoich opcji, należy ustawić je do rzeczywistych wartości godzinnych, podobny do tego:
<select id="tin" name="tin">
<option value="13">13:00</option>
<option value="19">19:00</option>
</select>
i ten
<select id="tout" name="tout">
<option value="12">12:00</option>
<option value="18">18:00</option>
</select>
Dzięki temu kod stanie się tak prosty, ponieważ do obliczenia daty i godziny zostaną użyte tylko natywne funkcje obiektu daty, z wyjątkiem sytuacji, gdy podzielone przez 24, aby uzyskać dni od godzin, podobne do:
var _MS_PER_HOUR = 1000 * 60 * 60; // 3600000ms per hour
var outputTextWithSign = '{0}{1} Days(s) {2}{3} Hours(s)'; // included sign
var outputTextWithoutSign = '{0} Days(s) {1} Hours(s)'; // no sign
$(function() {
$("#din, #dout").datepicker({
minDate: 0,
dateFormat: 'dd/mm/yy'
});
});
$(document).ready(function() {
$("#diff").focus(function() {
var startDate = $('#din').datepicker('getDate');
var endDate = $('#dout').datepicker('getDate');
// use native set hour to set the hours, assuming val is exact hours
// otherwise derive exact hour from your values
startDate.setHours($('#tin').val());
endDate.setHours($('#tout').val());
// convert date and time to UTC to take daylight savings into account
var utc1 = Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate(), startDate.getHours(), startDate.getMinutes(), startDate.getSeconds());
var utc2 = Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate(), endDate.getHours(), endDate.getMinutes(), endDate.getSeconds());
// get utc difference by always deducting less from more
// that way you always get accurate difference even if in reverse!
var utcDiff = utc1 > utc2 ? utc1 - utc2 : utc2 - utc1;
// get total difference in hours
var msDiff = Math.floor(utcDiff/_MS_PER_HOUR);
// get days from total hours
var dayDiff = Math.floor(msDiff/24);
// get left over hours after deducting full days
var hourDiff = Math.floor((msDiff - (24 * dayDiff)));
//
// write out results
//
// if you really need to show - sign and not just the difference...
var sign = utc1 > utc2 ? '-' : '';
// format output text - with (-)sign if required
var txtWithSign = outputTextWithSign.format(dayDiff ? sign : '', dayDiff, hourDiff ? sign : '', hourDiff);
// simple format without sign
var txtNoSign = outputTextWithoutSign.format(dayDiff, hourDiff);
// assign result - switch with/without sign result as needed
$("#diff").val(txtWithSign);
//$("#diff").val(txtNoSign);
});
});
// Helper for easy string formatting.
String.prototype.format = function() {
//var s = arguments[0];
var s = this;
for (var i = 0; i < arguments.length; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i]);
}
return s;
}
DEMO - Get dzień i godzina różnicy, biorąc pod uwagę oszczędności dzienne
Proszę opublikować tutaj swój kod – hindmost
@hindmost Stworzyłem skrzypce w tym całym moim kodu: http: // jsfiddle.net/u97Ja/2/ –
Zauważyłem już ten link w twoim kodzie. Jednak chciałbym zobaczyć twój kod tutaj: – hindmost