代码之家  ›  专栏  ›  技术社区  ›  AKS

如何找到两个日期之间的天数(仅考虑“网络/工作日”)(即不包括周末周六/周日)

  •  0
  • AKS  · 技术社区  · 4 年前

    7.5

    猛击

    微软Excel ,我可以使用网络天数来查找两个日期之间的天数。想知道是否可以将bash作为第一选择(或者在Linux上支持使用一行代码(我的第二个首选项)来解决这个问题的任何其他预装语言)。我不确定Linux中是否有任何库或自定义工具/实用工具可以计算此值。

    • 要计算两个日期之间的工作日数,可以使用NETWORKDAYS函数。NETWORKDAYS会自动排除周末,并且可以选择排除自定义假日列表。请注意,如果NETWORKDAYS是工作日,则在计算中包括开始日期和结束日期。

    我有一个文件.txt包含2个列字段 格式 断然的 起点 日期( 现在可以忽略标题行

    Resolved,StartOfWork
    2020-01-16,2020-01-10
    2020-01-13,2020-01-13
    2020-01-20,2020-01-15
    2020-01-20,2020-01-14
    2020-01-14,2020-01-09
    2020-01-09,2020-01-08
    2020-01-16,2020-01-14
    2020-01-09,2020-01-07
    2020-01-14,2020-01-12
    

    对于每一行,我要计算 在这两个日期之间( 解决/开始工作日期是否在周末:周六/周日无所谓

    • 天数的计算 不应包括 “周末,即。

    附言 :在这篇文章中,我的问题是 不同的 而不是这篇文章的要求: How to find the difference in days between two dates?

    0 回复  |  直到 4 年前
        1
  •  1
  •   tshiono    4 年前

    可能是吧 但这里有一个bash解决方案(如果感兴趣的话)。
    注意,它需要 -d date 命令。

    while IFS="," read -r endday startday; do
        if (( lineno++ == 0 )); then                # handle header line
            echo "Resolved,StartOfWork,TotalDates"
            continue
        fi
        startsec=$(date -d "$startday" +%s)
        startdayofweek=$(date -d "$startday" +%w)   # 0 for Sun, ... 6 for Sat
        endsec=$(date -d "$endday" +%s)
        days=$(( (endsec - startsec) / 86400 + 1 )) # calendar days
        weeks=$(( days / 7 ))                       # number of weeks
        frac=$(( days % 7 ))                        # fraction mod 7
        if (( startdayofweek == 0 )); then          # case of starting on Sunday
            if (( frac > 0 )); then
                add=1                               # additional number of holidays
            else
                add=0
            fi
        else
            magic=$(( frac + (startdayofweek + 6) % 7 ))
                                                    # calculate number of holidays
                                                    # in the fraction period
            if (( magic < 6 )); then
                add=0
            elif (( magic == 6 )); then
                add=1
            else
                add=2
            fi
        fi
        holidays=$(( weeks * 2 + add ))             # total number of holidays
        workdays=$(( days - holidays ))             # subtract the holidays
        echo "$endday,$startday,$workdays"
    done < inputfile
    
        2
  •  2
  •   Charles Duffy    4 年前

    我会打电话给Python解释器。采纳 Using Python to count the number of business days in a month? --

    countBusinessDaysPy=$(cat <<'EOF'
    import datetime, sys
    
    businessdays = 0
    startDate = datetime.date.fromisoformat(sys.argv[1])
    endDate = datetime.date.fromisoformat(sys.argv[2])
    if endDate < startDate:
        (startDate, endDate) = (endDate, startDate)
    
    while startDate <= endDate:      # change from <= to < to not count both start and end days
        if startDate.weekday() < 5:
            businessdays += 1
        startDate += datetime.timedelta(days=1)
    
    print(businessdays)
    EOF
    )
    
    countBusinessDays() { python3 -c "$countBusinessDaysPy" "$@"; }
    

    …提供了一个shell函数,该函数调用Python解释器进行所需的数学运算(注意,这是一个 范围)。此后:

    $ countBusinessDays 2019-01-01 2020-01-01
    262
    $ countBusinessDays 2019-01-01 2019-01-07
    5
    

    {
      read -r header; printf '%s\n' "$header,TotalDates"
      while IFS=, read -r resolved startOfWork rest; do
        printf '%s\n' "${resolved},${startOfWork}${rest:+,$rest},$(countBusinessDays "$startOfWork" "$resolved")"
      done
    } <yourInputFile
    

    …作为输出发射:

    Resolved,StartOfWork,TotalDates
    2020-01-16,2020-01-10,5
    2020-01-13,2020-01-13,1
    2020-01-20,2020-01-15,4
    2020-01-20,2020-01-14,5
    2020-01-14,2020-01-09,4
    2020-01-09,2020-01-08,2
    2020-01-16,2020-01-14,3
    2020-01-09,2020-01-07,3
    2020-01-14,2020-01-12,2