本文整理汇总了VB.NET中System.Globalization.DateTimeFormatInfo类的典型用法代码示例。如果您正苦于以下问题:VB.NET DateTimeFormatInfo类的具体用法?VB.NET DateTimeFormatInfo怎么用?VB.NET DateTimeFormatInfo使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DateTimeFormatInfo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
' 导入命名空间
Imports System.Globalization
Imports System.Reflection
Module Example
Public Sub Main()
' Get the properties of an en-US DateTimeFormatInfo object.
Dim dtfi As DateTimeFormatInfo = CultureInfo.GetCultureInfo("en-US").DateTimeFormat
Dim typ As Type = dtfi.GetType()
Dim props() As PropertyInfo = typ.GetProperties()
Dim value As Date = #05/28/2012 11:35AM#
For Each prop In props
' Is this a format pattern-related property?
If prop.Name.Contains("Pattern") Then
Dim fmt As String = CStr(prop.GetValue(dtfi, Nothing))
Console.WriteLine("{0,-33} {1} {2}{3,-37}Example: {4}",
prop.Name + ":", fmt, vbCrLf, "",
value.ToString(fmt))
Console.WriteLine()
End If
Next
End Sub
End Module
输出:
FullDateTimePattern: dddd, MMMM dd, yyyy h:mm:ss tt Example: Monday, May 28, 2012 11:35:00 AM LongDatePattern: dddd, MMMM dd, yyyy Example: Monday, May 28, 2012 LongTimePattern: h:mm:ss tt Example: 11:35:00 AM MonthDayPattern: MMMM dd Example: May 28 RFC1123Pattern: ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Example: Mon, 28 May 2012 11:35:00 GMT ShortDatePattern: M/d/yyyy Example: 5/28/2012 ShortTimePattern: h:mm tt Example: 11:35 AM SortableDateTimePattern: yyyy'-'MM'-'dd'T'HH':'mm':'ss Example: 2012-05-28T11:35:00 UniversalSortableDateTimePattern: yyyy'-'MM'-'dd HH':'mm':'ss'Z' Example: 2012-05-28 11:35:00Z YearMonthPattern: MMMM, yyyy Example: May, 2012
示例2:
Dim dtfi As System.Globalization.DateTimeFormatInfo
dtfi = System.Globalization.DateTimeFormatInfo.InvariantInfo
Console.WriteLine(dtfi.IsReadOnly)
dtfi = New System.Globalization.DateTimeFormatInfo()
Console.WriteLine(dtfi.IsReadOnly)
dtfi = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat
Console.WriteLine(dtfi.IsReadOnly)
输出:
True False True
示例3:
Dim ci As System.Globalization.CultureInfo = Nothing
Dim dtfi As System.Globalization.DateTimeFormatInfo = Nothing
' Instantiate a culture using CreateSpecificCulture.
ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
dtfi = ci.DateTimeFormat
Console.WriteLine("{0} from CreateSpecificCulture: {1}", ci.Name, dtfi.IsReadOnly)
' Instantiate a culture using the CultureInfo constructor.
ci = new System.Globalization.CultureInfo("en-CA")
dtfi = ci.DateTimeFormat
Console.WriteLine("{0} from CultureInfo constructor: {1}", ci.Name, dtfi.IsReadOnly)
' Retrieve a culture by calling the GetCultureInfo method.
ci = System.Globalization.CultureInfo.GetCultureInfo("en-AU")
dtfi = ci.DateTimeFormat
Console.WriteLine("{0} from GetCultureInfo: {1}", ci.Name, dtfi.IsReadOnly)
' Instantiate a DateTimeFormatInfo object by calling DateTimeFormatInfo.GetInstance.
ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB")
dtfi = System.Globalization.DateTimeFormatInfo.GetInstance(ci)
Console.WriteLine("{0} from GetInstance: {1}", ci.Name, dtfi.IsReadOnly)
输出:
en-US from CreateSpecificCulture: False en-CA from CultureInfo constructor: False en-AU from GetCultureInfo: True en-GB from GetInstance: False
示例4:
Dim specific, neutral As System.Globalization.CultureInfo
Dim dtfi As System.Globalization.DateTimeFormatInfo
' Instantiate a culture by creating a specific culture and using its Parent property.
specific = System.Globalization.CultureInfo.GetCultureInfo("fr-FR")
neutral = specific.Parent
dtfi = neutral.DateTimeFormat
Console.WriteLine("{0} from Parent property: {1}", neutral.Name, dtfi.IsReadOnly)
dtfi = System.Globalization.CultureInfo.GetCultureInfo("fr-FR").Parent.DateTimeFormat
Console.WriteLine("{0} from Parent property: {1}", neutral.Name, dtfi.IsReadOnly)
' Instantiate a neutral culture using the CultureInfo constructor.
neutral = New System.Globalization.CultureInfo("fr")
dtfi = neutral.DateTimeFormat
Console.WriteLine("{0} from CultureInfo constructor: {1}", neutral.Name, dtfi.IsReadOnly)
' Instantiate a culture using CreateSpecificCulture.
neutral = System.Globalization.CultureInfo.CreateSpecificCulture("fr")
dtfi = neutral.DateTimeFormat
Console.WriteLine("{0} from CreateSpecificCulture: {1}", neutral.Name, dtfi.IsReadOnly)
' Retrieve a culture by calling the GetCultureInfo method.
neutral = System.Globalization.CultureInfo.GetCultureInfo("fr")
dtfi = neutral.DateTimeFormat
Console.WriteLine("{0} from GetCultureInfo: {1}", neutral.Name, dtfi.IsReadOnly)
' Instantiate a DateTimeFormatInfo object by calling GetInstance.
neutral = System.Globalization.CultureInfo.CreateSpecificCulture("fr")
dtfi = System.Globalization.DateTimeFormatInfo.GetInstance(neutral)
Console.WriteLine("{0} from GetInstance: {1}", neutral.Name, dtfi.IsReadOnly)
' The example displays the following output:
' fr from Parent property: False
' fr from Parent property: False
' fr from CultureInfo constructor: False
' fr-FR from CreateSpecificCulture: False
' fr from GetCultureInfo: True
' fr-FR from GetInstance: False
示例5: Example
' 导入命名空间
Imports System.Collections
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Reflection
Module Example
Public Sub Main()
' Get all the neutral cultures
Dim names As New List(Of String)
Array.ForEach(CultureInfo.GetCultures(CultureTypes.NeutralCultures),
Sub(culture) names.Add(culture.Name))
names.Sort()
For Each name In names
' Ignore the invariant culture.
If name = "" Then Continue For
ListSimilarChildCultures(name)
Next
End Sub
Private Sub ListSimilarChildCultures(name As String)
' Create the neutral DateTimeFormatInfo object.
Dim dtfi As DateTimeFormatInfo = CultureInfo.GetCultureInfo(name).DateTimeFormat
' Retrieve all specific cultures of the neutral culture.
Dim cultures() As CultureInfo = Array.FindAll(CultureInfo.GetCultures(CultureTypes.SpecificCultures),
Function(culture) culture.Name.StartsWith(name + "-", StringComparison.OrdinalIgnoreCase))
' Create an array of DateTimeFormatInfo properties
Dim properties() As PropertyInfo = GetType(DateTimeFormatInfo).GetProperties(BindingFlags.Instance Or BindingFlags.Public)
Dim hasOneMatch As Boolean = False
For Each ci In cultures
Dim match As Boolean = True
' Get the DateTimeFormatInfo for a specific culture.
Dim specificDtfi As DateTimeFormatInfo = ci.DateTimeFormat
' Compare the property values of the two.
For Each prop In properties
' We're not interested in the value of IsReadOnly.
If prop.Name = "IsReadOnly" Then Continue For
' For arrays, iterate the individual elements to see if they are the same.
If prop.PropertyType.IsArray Then
Dim nList As IList = CType(prop.GetValue(dtfi, Nothing), IList)
Dim sList As IList = CType(prop.GetValue(specificDtfi, Nothing), IList)
If nList.Count <> sList.Count Then
match = false
Console.WriteLine(" Different n in {2} array for {0} and {1}", name, ci.Name, prop.Name)
Exit For
End If
For ctr As Integer = 0 To nList.Count - 1
If Not nList(ctr).Equals(sList(ctr))
match = false
Console.WriteLine(" {0} value different for {1} and {2}", prop.Name, name, ci.Name)
Exit For
End If
Next
If Not match Then Exit For
' Get non-array values.
Else
Dim specificValue As Object = prop.GetValue(specificDtfi)
Dim neutralValue As Object = prop.GetValue(dtfi)
' Handle comparison of Calendar objects.
If prop.Name = "Calendar" Then
' The cultures have a different calendar type.
If specificValue.ToString() <> neutralValue.ToString() Then
Console.WriteLine(" Different calendar types for {0} and {1}", name, ci.Name)
match = False
Exit For
End If
If TypeOf specificValue Is GregorianCalendar Then
If CType(specificValue, GregorianCalendar).CalendarType <> CType(neutralValue, GregorianCalendar).CalendarType Then
Console.WriteLine(" Different Gregorian calendar types for {0} and {1}", name, ci.Name)
match = False
Exit For
End If
End If
Else If Not specificValue.Equals(neutralValue) Then
match = false
Console.WriteLine(" Different {0} values for {1} and {2}", prop.Name, name, ci.Name)
Exit For
End If
End If
Next
If match Then
Console.WriteLine("DateTimeFormatInfo object for '{0}' matches '{1}'",
name, ci.Name)
hasOneMatch = True
End If
Next
If Not hasOneMatch Then
Console.WriteLine("DateTimeFormatInfo object for '{0}' --> No Match", name)
End If
Console.WriteLine()
End Sub
End Module
示例6:
Dim dtfi As DateTimeFormatInfo
dtfi = DateTimeFormatInfo.CurrentInfo
Console.WriteLine(dtfi.IsReadOnly)
dtfi = CultureInfo.CurrentCulture.DateTimeFormat
Console.WriteLine(dtfi.IsReadOnly)
dtfi = DateTimeFormatInfo.GetInstance(CultureInfo.CurrentCulture)
Console.WriteLine(dtfi.IsReadOnly)
输出:
True True True
示例7: Example
' 导入命名空间
Imports System.Globalization
Module Example
Public Sub Main()
Dim current1 As DateTimeFormatInfo = DateTimeFormatInfo.CurrentInfo
current1 = CType(current1.Clone(), DateTimeFormatInfo)
Console.WriteLine(current1.IsReadOnly)
Dim culture2 As CultureInfo = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name)
Dim current2 As DateTimeFormatInfo = culture2.DateTimeFormat
Console.WriteLine(current2.IsReadOnly)
End Sub
End Module
输出:
False False
示例8: Example
' 导入命名空间
Imports System.Globalization
Module Example
Public Sub Main()
Dim culture As CultureInfo
Dim dtfi As DateTimeFormatInfo
culture = CultureInfo.CurrentCulture
dtfi = culture.DateTimeFormat
Console.WriteLine("Culture Name: {0}", culture.Name)
Console.WriteLine("User Overrides: {0}", culture.UseUserOverride)
Console.WriteLine("Long Time Pattern: {0}", culture.DateTimeFormat.LongTimePattern)
Console.WriteLine()
culture = New CultureInfo(CultureInfo.CurrentCulture.Name, False)
Console.WriteLine("Culture Name: {0}", culture.Name)
Console.WriteLine("User Overrides: {0}", culture.UseUserOverride)
Console.WriteLine("Long Time Pattern: {0}", culture.DateTimeFormat.LongTimePattern)
End Sub
End Module
输出:
Culture Name: en-US User Overrides: True Long Time Pattern: HH:mm:ss Culture Name: en-US User Overrides: False Long Time Pattern: h:mm:ss tt
示例9: Main
' 导入命名空间
Imports System.Globalization
Public Class CurrentCultureFormatProvider : Implements IFormatProvider
Public Function GetFormat(formatType As Type) As Object _
Implements IFormatProvider.GetFormat
Console.WriteLine("Requesting an object of type {0}",
formatType.Name)
If formatType Is GetType(NumberFormatInfo) Then
Return NumberFormatInfo.CurrentInfo
Else If formatType Is GetType(DateTimeFormatInfo) Then
Return DateTimeFormatInfo.CurrentInfo
Else
Return Nothing
End If
End Function
End Class
Module Example
Public Sub Main()
Dim dateValue As New Date(2013, 05, 28, 13, 30, 0)
Dim value As String = dateValue.ToString("F", New CurrentCultureFormatProvider())
Console.WriteLine(value)
Console.WriteLine()
Dim composite As String = String.Format(New CurrentCultureFormatProvider,
"Date: {0:d} Amount: {1:C} Description: {2}",
dateValue, 1264.03d, "Service Charge")
Console.WriteLine(composite)
Console.WriteLine()
End Sub
End Module
输出:
Requesting an object of type DateTimeFormatInfo Tuesday, May 28, 2013 1:30:00 PM Requesting an object of type ICustomFormatter Requesting an object of type DateTimeFormatInfo Requesting an object of type NumberFormatInfo Date: 5/28/2013 Amount: $1,264.03 Description: Service Charge
示例10: Example
' 导入命名空间
Imports System.Globalization
Module Example
Public Sub Main()
Dim dateValue As New Date(2013, 08, 18)
Dim enUS As CultureInfo = CultureInfo.CreateSpecificCulture("en-US")
Dim dtfi As DateTimeFormatInfo = enUS.DateTimeFormat
Console.WriteLine("Before modifying DateTimeFormatInfo object: ")
Console.WriteLine("{0}: {1}", dtfi.ShortDatePattern,
dateValue.ToString("d", enUS))
Console.WriteLine()
' Modify the short date pattern.
dtfi.ShortDatePattern = "yyyy-MM-dd"
Console.WriteLine("After modifying DateTimeFormatInfo object: ")
Console.WriteLine("{0}: {1}", dtfi.ShortDatePattern,
dateValue.ToString("d", enUS))
End Sub
End Module
输出:
Before modifying DateTimeFormatInfo object: M/d/yyyy: 8/18/2013 After modifying DateTimeFormatInfo object: yyyy-MM-dd: 2013-08-18
示例11: Example
' 导入命名空间
Imports System.Globalization
Module Example
Public Sub Main()
Dim dateValue As New Date(2013, 08, 28)
Dim frFR As CultureInfo = CultureInfo.CreateSpecificCulture("fr-FR")
Dim dtfi As DateTimeFormatInfo = frFR.DateTimeFormat
Console.WriteLine("Before modifying DateSeparator property: {0}",
dateValue.ToString("g", frFR))
' Modify the date separator.
dtfi.DateSeparator = "-"
Console.WriteLine("After modifying the DateSeparator property: {0}",
dateValue.ToString("g", frFR))
End Sub
End Module
输出:
Before modifying DateSeparator property: 28/08/2013 00:00 After modifying the DateSeparator property: 28-08-2013 00:00
示例12: Example
' 导入命名空间
Imports System.Globalization
Module Example
Public Sub Main()
Dim value As New Date(2013, 7, 9)
Dim enUS As CultureInfo = CultureInfo.CreateSpecificCulture("en-US")
Dim dtfi As DateTimeFormatInfo = enUS.DateTimeFormat
Dim formats() As String = { "D", "F", "f" }
' Display date before modifying properties.
For Each fmt In formats
Console.WriteLine("{0}: {1}", fmt, value.ToString(fmt, dtfi))
Next
Console.WriteLine()
' We don't want to change the FullDateTimePattern, so we need to save it.
Dim originalFullDateTimePattern As String = dtfi.FullDateTimePattern
' Modify day name abbreviations and long date pattern.
dtfi.AbbreviatedDayNames = { "Su", "M", "Tu", "W", "Th", "F", "Sa" }
dtfi.LongDatePattern = "ddd dd-MMM-yyyy"
dtfi.FullDateTimePattern = originalFullDateTimePattern
For Each fmt In formats
Console.WriteLine("{0}: {1}", fmt, value.ToString(fmt, dtfi))
Next
End Sub
End Module
输出:
D: Tuesday, July 09, 2013 F: Tuesday, July 09, 2013 12:00:00 AM f: Tuesday, July 09, 2013 12:00 AM D: Tu 09-Jul-2013 F: Tuesday, July 09, 2013 12:00:00 AM f: Tu 09-Jul-2013 12:00 AM
示例13: Example
' 导入命名空间
Imports System.Globalization
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim enUS As CultureInfo = CultureInfo.CreateSpecificCulture("en-US")
Dim dtfi As DateTimeFormatInfo = enUS.DateTimeFormat
Console.WriteLine("Original Property Values:")
Console.WriteLine("ShortTimePattern: " + dtfi.ShortTimePattern)
Console.WriteLine("LongTimePattern: " + dtfi.LongTimePattern)
Console.WriteLine("FullDateTimePattern: " + dtfi.FullDateTimePattern)
Console.WriteLine()
dtfi.LongTimePattern = ReplaceWith24HourClock(dtfi.LongTimePattern)
dtfi.ShortTimePattern = ReplaceWith24HourClock(dtfi.ShortTimePattern)
Console.WriteLine("Modififed Property Values:")
Console.WriteLine("ShortTimePattern: " + dtfi.ShortTimePattern)
Console.WriteLine("LongTimePattern: " + dtfi.LongTimePattern)
Console.WriteLine("FullDateTimePattern: " + dtfi.FullDateTimePattern)
End Sub
Private Function ReplaceWith24HourClock(fmt As String) As String
Dim pattern As String = "^(?<openAMPM>\s*t+\s*)? " +
"(?(openAMPM) h+(?<nonHours>[^ht]+)$ " +
"| \s*h+(?<nonHours>[^ht]+)\s*t+)"
Return Regex.Replace(fmt, pattern, "HH${nonHours}", RegexOptions.IgnorePatternWhitespace)
End Function
End Module
输出:
Original Property Values: ShortTimePattern: h:mm tt LongTimePattern: h:mm:ss tt FullDateTimePattern: dddd, MMMM dd, yyyy h:mm:ss tt Modififed Property Values: ShortTimePattern: HH:mm LongTimePattern: HH:mm:ss FullDateTimePattern: dddd, MMMM dd, yyyy HH:mm:ss
示例14: Example
' 导入命名空间
Imports System.Globalization
Module Example
Public Sub Main()
Dim dateValue As New Date(2013, 5, 18, 13, 30, 0)
Dim formats() As String = { "D", "f", "F" }
Dim enUS As CultureInfo = CultureInfo.CreateSpecificCulture("en-US")
Dim dtfi As DateTimeFormatInfo = enUS.DateTimeFormat
Dim originalLongDatePattern As String = dtfi.LongDatePattern
' Display the default form of three long date formats.
For Each fmt In formats
Console.WriteLine(dateValue.ToString(fmt, dtfi))
Next
Console.WriteLine()
' Modify the long date pattern.
dtfi.LongDatePattern = originalLongDatePattern + " g"
For Each fmt In formats
Console.WriteLine(dateValue.ToString(fmt, dtfi))
Next
Console.WriteLine()
' Change A.D. to C.E. (for Common Era)
dtfi.LongDatePattern = originalLongDatePattern + " 'C.E.'"
For Each fmt In formats
Console.WriteLine(dateValue.ToString(fmt, dtfi))
Next
End Sub
End Module
输出:
Saturday, May 18, 2013 Saturday, May 18, 2013 1:30 PM Saturday, May 18, 2013 1:30:00 PM Saturday, May 18, 2013 A.D. Saturday, May 18, 2013 A.D. 1:30 PM Saturday, May 18, 2013 A.D. 1:30:00 PM Saturday, May 18, 2013 C.E. Saturday, May 18, 2013 C.E. 1:30 PM Saturday, May 18, 2013 C.E. 1:30:00 PM
示例15: Main
' 导入命名空间
Imports System.Globalization
Module Example
Public Sub Main()
Dim dateStrings() As String = { "08/18/2014", "01/02/2015" }
Dim cultureNames() As String = { "en-US", "en-GB", "fr-FR", "fi-FI" }
For Each cultureName In cultureNames
Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture(cultureName)
Console.WriteLine("Parsing strings using the {0} culture.",
culture.Name)
For Each dateStr In dateStrings
Try
Console.WriteLine(String.Format(culture,
" '{0}' --> {1:D}", dateStr,
DateTime.Parse(dateStr, culture)))
Catch e As FormatException
Console.WriteLine(" Unable to parse '{0}'", dateStr)
End Try
Next
Next
End Sub
End Module
输出:
Parsing strings using the en-US culture. 08/18/2014' --> Monday, August 18, 2014 01/02/2015' --> Friday, January 02, 2015 Parsing strings using the en-GB culture. Unable to parse '08/18/2014' 01/02/2015' --> 01 February 2015 Parsing strings using the fr-FR culture. Unable to parse '08/18/2014' 01/02/2015' --> dimanche 1 février 2015 Parsing strings using the fi-FI culture. Unable to parse '08/18/2014' 01/02/2015' --> 1. helmikuuta 2015