0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VBScript用Includeモジュール(日付ライブラリ)

Last updated at Posted at 2024-08-12
DateModule.vbs
Option Explicit

'// 対象日のリスト(文字列)を返す

Function GetOnDateStr()

    GetOnDateStr = "20230401 20230403 20230405"

End Function
'// 土日祝日なら実行しない

Function QuitInHoliday(ByVal tgt_date)

    If IsHoliday(tgt_date) Then WScript.Quit

End Function
'// 日付を fmt 形式の文字列で返す
'// "yyyy"は西暦4桁、"yy"は西暦下2桁、"y"は和暦、"0y"は和暦0付き、gは元号(明治以前は西暦)
'// "m"、"d"、"h"、"n"、"s" は 月、日、時、分、秒(二文字の場合は0つき2桁)
'// "aaa"は曜日、"a"はその略字

Function DateFormat(ByVal date_value, ByVal fmt)

    Dim yyyy, yy, y, g, mm, m, dd, d, hh, h, nn, n, ss, s, aaa, a

    yyyy = Year(date_value)
    m = Month(date_value)
    d = Day(date_value)
    h = Hour(date_value)
    n = Minute(date_value)
    s = Second(date_value)
    aaa = WeekdayName(Weekday(date_value))

    yy = Right(yyyy, 2)
    mm = Right("0" & m, 2)
    dd = Right("0" & d, 2)
    hh = Right("0" & h, 2)
    nn = Right("0" & n, 2)
    ss = Right("0" & s, 2)
    a = Left(aaa, 1)

    fmt = Replace(fmt, "yyyy", yyyy)
    fmt = Replace(fmt, "yy", yy)

    If date_value < #1868/9/4# Then g = "": y = yyyy
    If date_value >= #1868/9/4# Then g = "明治": y = yyyy - 1867
    If date_value >= #1912/7/31# Then g = "大正": y = yyyy - 1911
    If date_value >= #1926/12/27# Then g = "昭和": y = yyyy - 1925
    If date_value >= #1989/1/8# Then g = "平成": y = yyyy - 1988
    If date_value >= #2019/5/1# Then g = "令和": y = yyyy - 2018

    fmt = Replace(fmt, "0y", g & Right("0" & y, 2))
    If y = 1 Then y = "元"
    fmt = Replace(fmt, "y", g & y)

    fmt = Replace(fmt, "mm", mm)
    fmt = Replace(fmt, "dd", dd)
    fmt = Replace(fmt, "hh", hh)
    fmt = Replace(fmt, "nn", nn)
    fmt = Replace(fmt, "ss", ss)
    fmt = Replace(fmt, "aaa", aaa)

    fmt = Replace(fmt, "m", m)
    fmt = Replace(fmt, "d", d)
    fmt = Replace(fmt, "h", h)
    fmt = Replace(fmt, "n", n)
    fmt = Replace(fmt, "s", s)
    fmt = Replace(fmt, "a", a)

    DateFormat = fmt

End Function
'// 開始日(start_date)から終了日(end_date)までの営業日数を返す

Function GetNetWorkDay(start_date, end_date)

    Dim n: n = 0

    Do
        If Not IsHoliday(start_date) Then n = n + 1
        start_date = start_date + 1
    Loop While start_date < end_date

    GetNetWorkDay = n

End Function
'// 起算日(start_date)から d 営業日後の日付を返す

Function GetWorkday(ByVal start_date, ByVal d)

    Dim n: n = 0

    If d > 0 Then
        '// d が正なら d 営業日後の日付を返す
        Do While n < d
            start_date = start_date + 1
            If IsHoliday(start_date) = False Then
                n = n + 1
            End If
        Loop
    Else
        '// d が負なら d 営業日前の日付を返す
        Do While n < d
            start_date = start_date - 1
            If IsHoliday(start_date) = False Then
                n = n - 1
            End If
        Loop
    End If

    GetWorkday = start_date

End Function
'// 与えられた日付が土日祝日かを判定する

Function IsHoliday(ByVal tgt_date)

    Dim holiday

    IsHoliday = False

    If Weekday(tgt_date) = 1 Then IsHoliday = True: Exit Function
    If Weekday(tgt_date) = 7 Then IsHoliday = True: Exit Function

    For Each holiday In GetHolidays
        If tgt_date = holiday Then IsHoliday = True: Exit Function
    Next

End Function
'// 祝日の配列を返す

Function GetHolidays()

    Dim i, holidayAry, holidays: holidays = ""

    '// VBScript には Collectionオブジェクトがない。
    '// CreateObject("System.Collections.ArrayList") を使えば、
    '// コレクションを作ることができるが、.Net Framework3.5 が必要。
    '// よって、日付を文字列データにして、Split関数で文字列データの
    '// 配列にしてから、DateValue関数でそれぞれの要素を日付データに変換して
    '// 日付(祝日)データの配列にする

    holidays = "2024/01/01"
    holidays = holidays & vbCrLf & "2024/01/02"
    holidays = holidays & vbCrLf & "2024/01/03"
    holidays = holidays & vbCrLf & "2024/01/08"
    holidays = holidays & vbCrLf & "2024/02/11"
    holidays = holidays & vbCrLf & "2024/02/12"
    holidays = holidays & vbCrLf & "2024/02/23"
    holidays = holidays & vbCrLf & "2024/03/20"
    holidays = holidays & vbCrLf & "2024/04/29"
    holidays = holidays & vbCrLf & "2024/05/03"
    holidays = holidays & vbCrLf & "2024/05/04"
    holidays = holidays & vbCrLf & "2024/05/05"
    holidays = holidays & vbCrLf & "2024/05/06"
    holidays = holidays & vbCrLf & "2024/07/15"
    holidays = holidays & vbCrLf & "2024/08/11"
    holidays = holidays & vbCrLf & "2024/08/12"
    holidays = holidays & vbCrLf & "2024/09/16"
    holidays = holidays & vbCrLf & "2024/09/22"
    holidays = holidays & vbCrLf & "2024/09/23"
    holidays = holidays & vbCrLf & "2024/10/14"
    holidays = holidays & vbCrLf & "2024/11/03"
    holidays = holidays & vbCrLf & "2024/11/04"
    holidays = holidays & vbCrLf & "2024/11/23"
    holidays = holidays & vbCrLf & "2024/12/29"
    holidays = holidays & vbCrLf & "2024/12/30"
    holidays = holidays & vbCrLf & "2024/12/31"
    holidays = holidays & vbCrLf & "2025/01/01"
    holidays = holidays & vbCrLf & "2025/01/02"
    holidays = holidays & vbCrLf & "2025/01/03"
    holidays = holidays & vbCrLf & "2025/01/13"
    holidays = holidays & vbCrLf & "2025/02/11"
    holidays = holidays & vbCrLf & "2025/02/23"
    holidays = holidays & vbCrLf & "2025/02/24"
    holidays = holidays & vbCrLf & "2025/03/20"
    holidays = holidays & vbCrLf & "2025/04/29"
    holidays = holidays & vbCrLf & "2025/05/03"
    holidays = holidays & vbCrLf & "2025/05/04"
    holidays = holidays & vbCrLf & "2025/05/05"
    holidays = holidays & vbCrLf & "2025/05/06"
    holidays = holidays & vbCrLf & "2025/07/21"
    holidays = holidays & vbCrLf & "2025/08/11"
    holidays = holidays & vbCrLf & "2025/09/15"
    holidays = holidays & vbCrLf & "2025/09/23"
    holidays = holidays & vbCrLf & "2025/10/13"
    holidays = holidays & vbCrLf & "2025/11/03"
    holidays = holidays & vbCrLf & "2025/11/23"
    holidays = holidays & vbCrLf & "2025/11/24"
    holidays = holidays & vbCrLf & "2025/12/29"
    holidays = holidays & vbCrLf & "2025/12/30"
    holidays = holidays & vbCrLf & "2025/12/31"
    holidays = holidays & vbCrLf & "2026/01/01"
    holidays = holidays & vbCrLf & "2026/01/02"
    holidays = holidays & vbCrLf & "2026/01/03"
    holidays = holidays & vbCrLf & "2026/01/12"
    holidays = holidays & vbCrLf & "2026/02/11"
    holidays = holidays & vbCrLf & "2026/02/23"
    holidays = holidays & vbCrLf & "2026/03/20"
    holidays = holidays & vbCrLf & "2026/04/29"
    holidays = holidays & vbCrLf & "2026/05/03"
    holidays = holidays & vbCrLf & "2026/05/04"
    holidays = holidays & vbCrLf & "2026/05/05"
    holidays = holidays & vbCrLf & "2026/05/06"
    holidays = holidays & vbCrLf & "2026/07/20"
    holidays = holidays & vbCrLf & "2026/08/11"
    holidays = holidays & vbCrLf & "2026/09/21"
    holidays = holidays & vbCrLf & "2026/09/22"
    holidays = holidays & vbCrLf & "2026/09/23"
    holidays = holidays & vbCrLf & "2026/10/12"
    holidays = holidays & vbCrLf & "2026/11/03"
    holidays = holidays & vbCrLf & "2026/11/23"
    holidays = holidays & vbCrLf & "2026/12/29"
    holidays = holidays & vbCrLf & "2026/12/30"
    holidays = holidays & vbCrLf & "2026/12/31"
    holidays = holidays & vbCrLf & "2027/01/01"
    holidays = holidays & vbCrLf & "2027/01/02"
    holidays = holidays & vbCrLf & "2027/01/03"

    holidayAry = Split(holidays, vbCrLf)

    For i = LBound(holidayAry) To UBound(holidayAry)
        holidayAry(i) = DateValue(holidayAry(i))
    Next

    GetHolidays = holidayAry

End Function
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?