美文网首页
js请假天数计算

js请假天数计算

作者: IT小翔翔 | 来源:发表于2019-07-12 12:05 被阅读0次

    HTML部分:

    <!DOCTYPE html>

    <html>

    <head>

    <meta charset="UTF-8">

    <title></title>

    </head>

    <body>

    <input type="button" name="time1" id="time" onclick="setDay()" style="width: 30px; height: 30px;" />

    <input type="text" name="DATA_6" id="days" style="height: 30px ;" />

    <input type="text" value="2018-2-28" id="beginDate" />

    <input type="text" value="2018-3-2" id="endDate" />

    <input type="text" value="9:13:15" id="beginTime"/>

    <input type="text" value="11:13:15" id="endtime" />

    JS部分:

    <script>

    var beginDate;

    var endDate;

    var beginTime;

    var endTime;

    //获取前段时间参数

    function getTimeParam() {

    beginDate = document.getElementById("beginDate").value;

    endDate =  document.getElementById("endDate").value;

    beginTime = document.getElementById("beginTime").value;

    endTime = document.getElementById("endtime").value;

    //格式化日期参数

    beginDate = beginDate.replace(/-/g, "/");

    endDate = endDate.replace(/-/g, "/");

    }

    function setDay() {

    //获取前端参数

    getTimeParam();

    //校验时间是否合法

    var validTime = isValidDateTime(beginDate, endDate, beginTime, endTime);

    if(validTime) {

    setDays(beginDate, endDate, beginTime, endTime)

    } else {

    alert("结束时间必须大于开始时间")

    }

    }

    function setDays(beginDate, endDate, beginTime, endTime) {

    document.getElementById("days").value = calculateDay(beginDate, endDate, beginTime, endTime);

    }

    function isValidDateTime(beginDate, endDate, beginTime, endTime) {

    var bDateTime = new Date(beginDate + " " + beginTime);

    var eDateTime = new Date(endDate + " " + endTime);

    if(eDateTime <= bDateTime.getTime()) {

    return false;

    } else {

    return true;

    }

    }

    function calculateDay(beginDate, endDate, beginTime, endTime) {

    var hour = 60 * 60 * 1000;

    var bTimeStr = beginTime.split(":");

    var eTimeStr = endTime.split(":");

    var bDate = new Date(beginDate);

    var eDate = new Date(endDate);

    var bTime = bTimeStr[0] * hour + bTimeStr[1] * 60 * 1000 + bTimeStr[2] * 1000;

    var eTime = eTimeStr[0] * hour + eTimeStr[1] * 60 * 1000 + eTimeStr[2] * 1000;

    var days = 0.0;

    var half = 0.5;

    var allDay = 1.0;

    var AM_UpHour = 8 * hour;

    var AM_DownHour = 12 * hour;

    var PM_UpHour = 13.5 * hour;

    var PM_DownHour = 17.5 * hour;

    //相隔天数

    var n = (eDate.getTime() - bDate.getTime()) / (hour * 24);

    if(n > 1) {

    days = n - 1;

    }

    //如果是同一天

    if(beginDate == endDate) {

    if(bTime < AM_DownHour) {

    if(eTime <= PM_UpHour) {

    days += half;

    } else {

    days += allDay;

    }

    } else {

    days += allDay;

    }

    }

    //不是同一天

    else {

    //计算开始的那一天的

    //如果开始时间<早上下班时间+1.0

    if(bTime < AM_DownHour) {

    days += allDay;

    }

    //如果是下午开始

    else {

    days += half;

    }

    //计算结束的那一天

    //如果是早上结束+0.5

    if(eTime <= PM_UpHour) {

    days += half;

    }

    //如果是下午结束+1.0

    else {

    days += allDay;

    }

    }

    return days;

    }

    </script>

    </body>

    </html>

    原文链接

    相关文章

      网友评论

          本文标题:js请假天数计算

          本文链接:https://www.haomeiwen.com/subject/ctjekctx.html