年月日時分秒を整形
日付を表すとき、日本では「年/月/日」ですが、国によっては「月/日/年」だったり「日/月/年」だったりと様々です。
また、日付の年を省略したい場合や時刻の秒を省略したい場合など、日時を表す形式は様々です。
以下のサンプルでは日付を任意のフォーマットに変換します。
Option Explicit
MsgBox fnFormatDateTime(NOW(),"YYYY/MM/DD HH:NN:SS")
Function fnFormatDateTime(dDate,strFormat)
Dim strDate
strDate = ""
If IsDate(dDate) Then
strDate = strFormat
strDate = Replace(strDate,"YYYY", Right(Year(dDate) ,4))
strDate = Replace(strDate,"YY", Right("0" & Year(dDate) ,2))
strDate = Replace(strDate,"MM", Right("0" & Month(dDate) ,2))
strDate = Replace(strDate,"DD", Right("0" & Day(dDate) ,2))
strDate = Replace(strDate,"HH", Right("0" & Hour(dDate) ,2))
strDate = Replace(strDate,"NN", Right("0" & Minute(dDate),2))
strDate = Replace(strDate,"SS", Right("0" & Second(dDate),2))
End If
fnFormatDateTime = strDate
End Function
上記の「YYYY/MM/DD HH:NN:SS」を「MM/DD/YYYY HH:NN」などに変えることでフォーマットを変えることができます。