$(function () {
var d = new Date();
var toDay = d.getDate();
var datepicked = function() {
var from = $('#from');
var to = $('#to');
var days = $('#days');
var fromDate = from.datepicker('getDate');
var toDate = to.datepicker('getDate');
if(toDate && fromDate){
if (toDate.getTime() < fromDate.getTime()){
alert('วันที่สิ้นสุดน้อยกว่าวันที่เริ่ม');
$('#datepickerthai_out').val('');
}
}
if (toDate && fromDate) {
var difference = 0 ;
var oneDay = 86400000; //ms per day
var difference = Math.ceil((toDate.getTime() - fromDate.getTime()) / oneDay +1 );
days.val(difference)
}
}
function noWeekends(date) {
var d = date.getDay();
return [d !== 0 && d !== 0, "", ""];
}
$("#from , #to").datepicker({
isBuddhist: true,
defaultDate: toDay,
onSelect: datepicked,
dateFormat: 'yy-mm-dd',
minDate: 0,
maxDate: "",
beforeShowDay: noWeekends
});
});
ผมอยากให้ใส่วันที่ แล้ว คำนวณเลยครับไม่ต้อง กด calc แล้วผลคำนวณมาโชร์ ใน text
code ครับ
Code (JavaScript)
<html>
<head>
<title>ThaiCreate.Com Tutorial</title>
</head>
<body>
<meta charset="utf-8">
<link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css">
<script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="jqueryui/style.css">
<script>
$(function() {
$( "#d1" ).datepicker({dateFormat:'yy-mm-dd'});
$( "#d2" ).datepicker({dateFormat:'yy-mm-dd'});
});
</script>
<script>
$(document).ready(function(){
$('#calc').click(function(){
var d1 = $('#d1').val();
var d2 = $('#d2').val();
$('#dif').text(workingDaysBetweenDates(d1,d2));
});
});
function workingDaysBetweenDates(d0, d1) {
var holidays = ['2018-08-13',''];
var startDate = parseDate(d0);
var endDate = parseDate(d1);
// Validate input
if (endDate < startDate) {
return 0;
}
// Calculate days between dates
var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
startDate.setHours(0,0,0,1); // Start just after midnight
endDate.setHours(23,59,59,999); // End just before midnight
var diff = endDate - startDate; // Milliseconds between datetime objects
var days = Math.ceil(diff / millisecondsPerDay);
// Subtract two weekend days for every week in between
var weeks = Math.floor(days / 7);
days -= weeks * 1;
// Handle special cases
var startDay = startDate.getDay();
var endDay = endDate.getDay();
// Remove weekend not previously removed.
if (startDay - endDay > 1) {
days -= 1;
}
// Remove start day if span starts on Sunday but ends before Saturday
if (startDay == 0 && endDay != 0) {
days--;
}
// Remove end day if span ends on Saturday but starts after Sunday
if (endDay == 0 && startDay != 0) {
days--;
}
/* Here is the code */
for (var i in holidays) {
if ((holidays[i] >= d0) && (holidays[i] <= d1)) {
days--;
}
}
return days;
}
function parseDate(input) {
// Transform date from text to date
var parts = input.match(/(\d+)/g);
// new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
return new Date(parts[0], parts[1]-1, parts[2]); // months are 0-based
}
</script>
<input type="text" id="d1" ><br>
<input type="text" id="d2" >
<input type="text" id="days" >
<p>Working days count: <span id="dif"></span></p>
<button id="calc">Calc</button>
<br>
กำลำหนดให้วันที่ 13 หยุดชดเชย
</body>
</html>