PDF Javascript计算日期之间的天数

时间:2012-04-30 06:20:46

标签: forms pdf

我找到了很多JavaScript脚本来计算2个日期之间的天数......这是我的并发症......

1 - 我需要删除周末

2 - 我需要在PDF表单中设置单元格内的值

此Javascript在PDF表单中运行

所以这里是如何运作的:

Cell#1 = FIRSTDAY

Cell#2 = LASTDAY

Cell#3 = TOTALDAYS

我需要计算两天之间的天差并取消周末......任何想法?

1 个答案:

答案 0 :(得分:1)

不确定从PDF表单字段中获取值/设置值,但此函数应计算两个日期之间的工作日数:

var startDate = new Date('04/30/2012');
var endDate = new Date('05/31/2012');
var weekDaysElapsed = getWeekDaysElapsed (startDate, endDate);

function getWeekDaysElapsed (startDate, endDate) {
    var oneDayInMillis = 1000 * 60 * 60 * 24;
    var elapsedDays = 0;
    if (startDate < endDate) {
        var day = startDate.getDay();
        if (day != 6 && day != 0) {   //6=Sat, 0=Sun
            elapsedDays = 1;
        }
        startDate = new Date(startDate.getTime() + oneDayInMillis);
        return elapsedDays + getWeekDaysElapsed (startDate, endDate);
    }
    return elapsedDays;
}

var startDate = new Date('04/30/2012'); var endDate = new Date('05/31/2012'); var weekDaysElapsed = getWeekDaysElapsed (startDate, endDate); function getWeekDaysElapsed (startDate, endDate) { var oneDayInMillis = 1000 * 60 * 60 * 24; var elapsedDays = 0; if (startDate < endDate) { var day = startDate.getDay(); if (day != 6 && day != 0) { //6=Sat, 0=Sun elapsedDays = 1; } startDate = new Date(startDate.getTime() + oneDayInMillis); return elapsedDays + getWeekDaysElapsed (startDate, endDate); } return elapsedDays; }