DateTimeStylesの空白に関するオプションには次のようなものがあります。
- AllowLeadingWhite
- 文字列中の日時に先行する空白文字を無視する。
- AllowTrailingWhite
- 文字列中の日時に後続する空白文字を無視する。
- AllowInnerWhite
- 文字列中の日時の途中に存在する空白文字を無視する。
- AllowWhiteSpaces
- 文字列中の日時に含まれる空白文字を無視する。
(上記のAllowLeadingWhite, AllowTrailingWhite, AllowInnerWhiteを組み合わせたものと同じ)
これらのオプションは厳密に書式を指定するParseExactメソッドで特に効果を発揮します。 書式だけでは定義できない空白の表記ゆれを許容するかどうかをこのオプションで指定することが出来ます。 次の表は、様々な個所に空白を含む文字列をParseExactメソッドで解析した場合の結果の違いをまとめたものです。
DateTime/DateTimeOffset.TryParseExactメソッドでDateTimeStylesを指定して文字列中の空白の扱いを指定する
Imports System
Imports System.Globalization
Class Sample
Shared Sub Main()
Dim format As String = "yyyy-M-d" ' "年-月-日"(0埋めなし)の書式
Dim inputs() As String = New String() { _
"2013-04-01", _
" 2013-04-01", _
"2013-04-01 ", _
" 2013-04-01 ", _
"2013- 4-01", _
"2013 - 4 - 1", _
" 2013 -4 - 1 " _
}
Dim styles() As DateTimeStyles = New DateTimeStyles() { _
DateTimeStyles.None, _
DateTimeStyles.AllowLeadingWhite, _
DateTimeStyles.AllowTrailingWhite, _
DateTimeStyles.AllowInnerWhite, _
DateTimeStyles.AllowWhiteSpaces _
}
' DateTimeStylesとDateTimeへの変換結果の違いを表示
Console.WriteLine("DateTime.ParseExact")
Console.WriteLine("{0,-20}|{1,-22}|{2,-22}|{3,-22}|{4,-22}|{5,-22}", _
"(input)", _
"None", _
"AllowLeadingWhite", _
"AllowTrailingWhite", _
"AllowInnerWhite", _
"AllowWhiteSpaces")
For Each input As String In inputs
Console.Write("{0,-20}|", "'" + input + "'")
For Each style As DateTimeStyles In styles
Dim dt As DateTime
If DateTime.TryParseExact(input, format, Nothing, style, dt)
Console.Write("{0,-22}|", dt)
Else
Console.Write("{0,-22}|", "(false)")
End If
Next
Console.WriteLine()
Next
Console.WriteLine()
' DateTimeStylesとDateTimeOffsetへの変換結果の違いを表示
Console.WriteLine("DateTimeOffset.ParseExact")
Console.WriteLine("{0,-20}|{1,-28}|{2,-28}|{3,-28}|{4,-28}|{5,-28}", _
"(input)", _
"None", _
"AllowLeadingWhite", _
"AllowTrailingWhite", _
"AllowInnerWhite", _
"AllowWhiteSpaces")
For Each input As String In inputs
Console.Write("{0,-20}|", "'" + input + "'")
For Each style As DateTimeStyles In styles
Dim dto As DateTimeOffset
If DateTimeOffset.TryParseExact(input, format, Nothing, DateTimeStyles.AssumeLocal Or style, dto) Then
Console.Write("{0,-28}|", dto)
Else
Console.Write("{0,-28}|", "(false)")
End If
Next
Console.WriteLine()
Next
Console.WriteLine()
End Sub
End Class
実行結果
DateTime.ParseExact (input) |None |AllowLeadingWhite |AllowTrailingWhite |AllowInnerWhite |AllowWhiteSpaces '2013-04-01' |2013/04/01 0:00:00 |2013/04/01 0:00:00 |2013/04/01 0:00:00 |2013/04/01 0:00:00 |2013/04/01 0:00:00 | ' 2013-04-01' |(false) |2013/04/01 0:00:00 |(false) |2013/04/01 0:00:00 |2013/04/01 0:00:00 | '2013-04-01 ' |(false) |(false) |2013/04/01 0:00:00 |(false) |2013/04/01 0:00:00 | ' 2013-04-01 ' |(false) |(false) |(false) |(false) |2013/04/01 0:00:00 | '2013- 4-01' |(false) |(false) |(false) |2013/04/01 0:00:00 |2013/04/01 0:00:00 | '2013 - 4 - 1' |(false) |(false) |(false) |2013/04/01 0:00:00 |2013/04/01 0:00:00 | ' 2013 -4 - 1 ' |(false) |(false) |(false) |(false) |2013/04/01 0:00:00 | DateTimeOffset.ParseExact (input) |None |AllowLeadingWhite |AllowTrailingWhite |AllowInnerWhite |AllowWhiteSpaces '2013-04-01' |2013/04/01 0:00:00 +09:00 |2013/04/01 0:00:00 +09:00 |2013/04/01 0:00:00 +09:00 |2013/04/01 0:00:00 +09:00 |2013/04/01 0:00:00 +09:00 | ' 2013-04-01' |(false) |2013/04/01 0:00:00 +09:00 |(false) |2013/04/01 0:00:00 +09:00 |2013/04/01 0:00:00 +09:00 | '2013-04-01 ' |(false) |(false) |2013/04/01 0:00:00 +09:00 |(false) |2013/04/01 0:00:00 +09:00 | ' 2013-04-01 ' |(false) |(false) |(false) |(false) |2013/04/01 0:00:00 +09:00 | '2013- 4-01' |(false) |(false) |(false) |2013/04/01 0:00:00 +09:00 |2013/04/01 0:00:00 +09:00 | '2013 - 4 - 1' |(false) |(false) |(false) |2013/04/01 0:00:00 +09:00 |2013/04/01 0:00:00 +09:00 | ' 2013 -4 - 1 ' |(false) |(false) |(false) |(false) |2013/04/01 0:00:00 +09:00 |
なお、この例では日付のみを解析する際に時刻をローカル時刻であると仮定するために、DateTimeOffset.ParseExactの呼び出し時にDateTimeStyles.AssumeLocalを指定しています。