現在日付を指定したフォーマットで取得する
GetCurrentDate関数
現在日付を引数で指定した日付フォーマットで取得する関数。※ ShowErrorMessage関数についてはリンク先をご参照ください。
'================================================== ' <summary> ' 現在日付を指定したフォーマットで取得する ' </summary> ' <param name="dateFormat">日付フォーマット</param> ' <returns>現在日付</returns> ' <remarks> ' 日付フォーマットがブランクの場合、yyyy/mm/dd形式で取得する ' </remarks> '================================================== Public Function GetCurrentDate(ByVal dateFormat As String) As String On Error GoTo Catch GetCurrentDate = "" If dateFormat <> "" Then GetCurrentDate = Format(Date, dateFormat) Else GetCurrentDate = Format(Date, "yyyy/mm/dd") End If Exit Function Catch: Call ShowErrorMessage("GetCurrentDate") End Function
実行方法
'================================================== ' <summary> ' GetCurrentDate関数のテスト ' </summary> '================================================== Sub TestGetCurrentDate() Dim result As String result = GetCurrentDate("yyyy年mm月dd日") result = result &amp; vbCrLf &amp; GetCurrentDate("") MsgBox result End Sub
【一般的な日付記号】
記号 | 範囲 |
---|---|
d | 1 〜 31 (前に 0 を付けない、月の日付) |
dd | 01 〜 31 (前に 0 を付ける、月の日付) |
w | 1 〜 7 (週の曜日。土曜日 = 1 から開始) |
ww | 1 〜 53 (1 年のうちで何週目かを表す数値。前に 0 を付けず、1 月 1 日から開始。) |
m | 1 〜 12 (月。前に 0 を付けず、1 月 = 1 から開始。) |
mm | 01 〜 12 (月。前に 0 を付け、1 月 = 01 から開始。) |
mmm | 月の省略名を表示 (イスラム暦の月の名前には省略形はありません) |
mmmm | 完全な月名を表示 |
y | 1 〜 366 (1 年のうちで何日目かを表す数値) |
yy | 00 〜 99 (西暦年の下 2 桁) |
yyyy | 100 〜 9999 (3 桁または 4 桁の西暦年) |
実行結果
