VBScriptはWindows上で手軽に動かせるスクリプト言語です。
本稿では、ユーザーが入力した西暦日付(例:2025/04/17)の 年・月・日 情報をもとに、日本の和暦の 何年何月何日 に相当するかを判定し、正確に表示する方法を解説します。
日付単位での境界処理を学ぶことで、VBScriptのDate型や日付演算の理解が深まります。
VBScriptで日付を扱う方法
- Date型: VBScriptではCDate()関数で文字列を日付に変換し、Date型で扱います。
- Year/Month/Day関数: Date型から年・月・日を抽出できます。
- DateSerial関数: 年月日を指定してDate型を生成します。
Dim d : d = CDate("2025/04/17") ' Date型となる
Dim y : y = Year(d) ' 2025を取得
Dim m : m = Month(d) ' 4を取得
Dim da: da = Day(d) ' 17を取得
元号の開始日一覧(年・月・日)
元号 | 開始日 | 終了日* | 備考 |
---|---|---|---|
令和 | 2019年5月1日 | — | 現行元号 |
平成 | 1989年1月8日 | 2019年4月30日 | 1989/1/8開始、2019/4/30終了 |
昭和 | 1926年12月25日 | 1989年1月7日 | 1926/12/25開始、1989/1/7終了 |
大正 | 1912年7月30日 | 1926年12月24日 | 1912/7/30開始、1926/12/24終了 |
* 各元号の終期は翌元号開始日の前日です。日付境界を超えないよう注意しましょう。
日付判定ロジックの解説
- InputBoxで入力した文字列をCDateでDate型に変換する。
- DateSerialで各元号の開始日をDate型で用意。
- 入力日付と各開始日を>=や<演算子で比較し、該当する元号を決定。
- 元号開始年との差を計算し、元号何年かを算出。
- 同じ月日以前か以後かで「元年」「2年」などを判定。
' 判定例(疑似コード)
If inputDate >= DateSerial(2019,5,1) Then
era = "令和"
eraYear = Year(inputDate) - 2018
ElseIf inputDate >= DateSerial(1989,1,8) Then
era = "平成"
eraYear = Year(inputDate) - 1988
ElseIf inputDate >= DateSerial(1926,12,25) Then
era = "昭和"
eraYear = Year(inputDate) - 1925
ElseIf inputDate >= DateSerial(1912,7,30) Then
era = "大正"
eraYear = Year(inputDate) - 1911
Else
' 対応外
End If
サンプルコード全文
Option Explicit
Dim strInput, inputDate
Dim era, eraYear
Dim resultMsg
' 1. 日付入力
strInput = InputBox("日付を西暦で入力してください(例: 2025/04/17)", "西暦→元号変換(日付対応版]")
If strInput = "" Then
MsgBox "入力がキャンセルされました。", vbExclamation
WScript.Quit
End If
' 2. 文字列を日付に変換
On Error Resume Next
inputDate = CDate(strInput)
If Err.Number <> 0 Then
MsgBox "正しい日付形式(YYYY/MM/DD)を入力してください。", vbCritical
WScript.Quit
End If
On Error GoTo 0
' 3. 元号判定と年計算
If inputDate >= DateSerial(2019,5,1) Then
era = "令和"
eraYear = Year(inputDate) - 2018
ElseIf inputDate >= DateSerial(1989,1,8) Then
era = "平成"
eraYear = Year(inputDate) - 1988
ElseIf inputDate >= DateSerial(1926,12,25) Then
era = "昭和"
eraYear = Year(inputDate) - 1925
ElseIf inputDate >= DateSerial(1912,7,30) Then
era = "大正"
eraYear = Year(inputDate) - 1911
Else
MsgBox "大正以前は対応していません。", vbInformation
WScript.Quit
End If
' 4. 結果メッセージ生成
If eraYear = 1 Then
resultMsg = era & "元年 " & Month(inputDate) & "月" & Day(inputDate) & "日"
Else
resultMsg = era & eraYear & "年 " & Month(inputDate) & "月" & Day(inputDate) & "日"
End If
' 5. 表示
MsgBox resultMsg, vbInformation, "変換結果"
実行例
- 入力: 2019/05/01 → 結果: 「令和元年 5月1日」
- 入力: 1989/01/07 → 結果: 「昭和64年 1月7日」
- 入力: 1989/01/08 → 結果: 「平成1年 1月8日」
応用ポイント
- 終期の日付判定
特定の元号終了日をまたいだ範囲で処理する場合、<
演算子を使って終了日より前の日付を判定可能。 - 配列サブルーチン
演習3のように配列処理を適用し、日付文字列配列の一括変換も可能。
演習問題と解答例
問題1:入力フォーマットチェック
YYYY/MM/DD以外の形式を検知してエラーとするコードを追加してください。
解答例1
' 入力直後にチェック
If Not strInput Like "####/##/##" Then
MsgBox "形式は YYYY/MM/DD のみ許可です。", vbCritical
WScript.Quit
End If
問題2:元号終了日も厳密に対応
各元号の終了日をDateSerialで取得し、入力日が終了日を超えていたら次元号扱いになるように調整してください。
解答例2
Dim endDateReiwa, endDateHeisei, endDateShowa
endDateReiwa = #12/31/9999# ' 上限
endDateHeisei = DateSerial(2019,4,30)
endDateShowa = DateSerial(1989,1,7)
' 判定時に終了日チェックを追加
If inputDate >= DateSerial(2019,5,1) And inputDate <= endDateReiwa Then
'…省略…
ElseIf inputDate >= DateSerial(1989,1,8) And inputDate <= endDateHeisei Then
'…省略…
問題3:複数日付の一括変換サブルーチン
配列 Array(“1926/12/24″,”2019/04/30″,”2025/04/17”) を渡して、結果を連続でMsgBox表示するサブルーチンを作成してください。
解答例3
Sub ConvertEraDates(dateArr)
Dim i, d
For i = LBound(dateArr) To UBound(dateArr)
d = CDate(dateArr(i))
'(先の判定ロジック)
MsgBox dateArr(i) & " → " & resultMsg, vbInformation
Next
End Sub
Dim dates
dates = Array("1926/12/24","2019/04/30","2025/04/17")
ConvertEraDates dates
まとめ
本稿では、VBScriptで 年・月・日 単位の精密な日付判定を使い、西暦を元号の年号に変換する方法を学びました。
日付演算や条件分岐のテクニックを身につけ、さらに実践的なスクリプトを作成しましょう。