本文整理汇总了VB.NET中System.DateTime结构体的典型用法代码示例。如果您正苦于以下问题:VB.NET DateTime结构体的具体用法?VB.NET DateTime怎么用?VB.NET DateTime使用的例子?那么, 这里精选的结构体代码示例或许可以为您提供帮助。
在下文中一共展示了DateTime结构体的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1:
Dim date1 As Date = Date.Now
Dim date2 As Date = Date.UtcNow
Dim date3 As Date = Date.Today
示例2:
Dim dateString As String = "5/1/2008 8:30:52 AM"
Dim date1 As Date = Date.Parse(dateString,
System.Globalization.CultureInfo.InvariantCulture)
Dim iso8601String As String = "20080501T08:30:52Z"
Dim dateISO8602 As Date = DateTime.ParseExact(iso8601String, "yyyyMMddTHH:mm:ssZ",
System.Globalization.CultureInfo.InvariantCulture)
Console.WriteLine(dateISO8602)
示例3:
Dim date1 As Date = #3/1/2008 7:00AM#
Console.WriteLine(date1.ToString())
' For en-US culture, displays 3/1/2008 7:00:00 AM
示例4:
Dim date1 As Date = #3/1/2008 7:00AM#
Console.WriteLine(date1.ToString("F"))
' Displays Saturday, March 01, 2008 7:00:00 AM
示例5: formats
Dim formats() As String = {"yyyyMMdd", "HHmmss"}
Dim dateStrings() As String = {"20130816", "20131608",
" 20130816 ", "115216",
"521116", " 115216 "}
Dim parsedDate As DateTime
For Each dateString As String In dateStrings
If DateTime.TryParseExact(dateString, formats, Nothing,
DateTimeStyles.AllowWhiteSpaces Or
DateTimeStyles.AdjustToUniversal,
parsedDate) Then
Console.WriteLine($"{dateString} --> {parsedDate:g}")
Else
Console.WriteLine($"Cannot convert {dateString}")
End If
Next
输出:
20130816 --> 8/16/2013 12:00 AM Cannot convert 20131608 20130816 --> 8/16/2013 12:00 AM 115216 --> 4/22/2013 11:52 AM Cannot convert 521116 115216 --> 4/22/2013 11:52 AM
示例6: CultureInfo
Dim thTH As New CultureInfo("th-TH")
Dim value As New DateTime(2016, 5, 28)
Console.WriteLine(value.ToString(thTH))
thTH.DateTimeFormat.Calendar = New GregorianCalendar()
Console.WriteLine(value.ToString(thTH))
输出:
28/5/2559 0:00:00 28/5/2016 0:00:00
示例7: ThaiBuddhistEraParse
Private Sub ThaiBuddhistEraParse()
Dim thTH As New CultureInfo("th-TH")
Dim value As DateTime = DateTime.Parse("28/5/2559", thTH)
Console.WriteLine(value.ToString(thTH))
thTH.DateTimeFormat.Calendar = New GregorianCalendar()
Console.WriteLine(value.ToString(thTH))
' The example displays the following output:
' 28/5/2559 0:00:00
' 28/5/2016 0:00:00
End Sub
示例8: CultureInfo
Dim thTH As New CultureInfo("th-TH")
Dim dat As New DateTime(2559, 5, 28, thTH.DateTimeFormat.Calendar)
Console.WriteLine($"Thai Buddhist Era date: {dat.ToString("d", thTH)}")
Console.WriteLine($"Gregorian date: {dat:d}")
输出:
Thai Buddhist Era Date: 28/5/2559 Gregorian Date: 28/05/2016
示例9: CultureInfo
Dim thTH As New CultureInfo("th-TH")
Dim cal As Calendar = thTH.DateTimeFormat.Calendar
Dim dat As New DateTime(2559, 5, 28, cal)
Console.WriteLine("Using the Thai Buddhist Era calendar:")
Console.WriteLine($"Date: {dat.ToString("d", thTH)}")
Console.WriteLine($"Year: {cal.GetYear(dat)}")
Console.WriteLine($"Leap year: {cal.IsLeapYear(cal.GetYear(dat))}")
Console.WriteLine()
Console.WriteLine("Using the Gregorian calendar:")
Console.WriteLine($"Date: {dat:d}")
Console.WriteLine($"Year: {dat.Year}")
Console.WriteLine($"Leap year: {DateTime.IsLeapYear(dat.Year)}")
输出:
Using the Thai Buddhist Era calendar Date : 28/5/2559 Year: 2559 Leap year : True Using the Gregorian calendar Date : 28/05/2016 Year: 2016 Leap year : True
示例10: CultureInfo
Dim thTH As New CultureInfo("th-TH")
Dim thCalendar As Calendar = thTH.DateTimeFormat.Calendar
Dim dat As New DateTime(1395, 8, 18, thCalendar)
Console.WriteLine("Using the Thai Buddhist Era calendar:")
Console.WriteLine($"Date: {dat.ToString("d", thTH)}")
Console.WriteLine($"Day of Week: {thCalendar.GetDayOfWeek(dat)}")
Console.WriteLine($"Week of year: {thCalendar.GetWeekOfYear(dat, CalendarWeekRule.FirstDay, DayOfWeek.Sunday)}")
Console.WriteLine()
Dim greg As Calendar = New GregorianCalendar()
Console.WriteLine("Using the Gregorian calendar:")
Console.WriteLine($"Date: {dat:d}")
Console.WriteLine($"Day of Week: {dat.DayOfWeek}")
Console.WriteLine($"Week of year: {greg.GetWeekOfYear(dat, CalendarWeekRule.FirstDay, DayOfWeek.Sunday)}")
输出:
Using the Thai Buddhist Era calendar Date : 18/8/1395 Day of Week: Sunday Week of year: 34 Using the Gregorian calendar Date : 18/08/0852 Day of Week: Sunday Week of year: 34
示例11: PersistAsLocalStrings
Public Sub PersistAsLocalStrings()
SaveDatesAsStrings()
RestoreDatesAsStrings()
End Sub
Private Sub SaveDatesAsStrings()
Dim dates As Date() = {#6/14/2014 6:32AM#, #7/10/2014 11:49PM#,
#1/10/2015 1:16AM#, #12/20/2014 9:45PM#,
#6/2/2014 3:14PM#}
Dim output As String = Nothing
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For ctr As Integer = 0 To dates.Length - 1
Console.WriteLine(dates(ctr).ToString("f"))
output += dates(ctr).ToString() + If(ctr <> dates.Length - 1, "|", "")
Next
Dim sw As New StreamWriter(filenameTxt)
sw.Write(output)
sw.Close()
Console.WriteLine("Saved dates...")
End Sub
Private Sub RestoreDatesAsStrings()
TimeZoneInfo.ClearCachedData()
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Dim sr As New StreamReader(filenameTxt)
Dim inputValues As String() = sr.ReadToEnd().Split({"|"c}, StringSplitOptions.RemoveEmptyEntries)
sr.Close()
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For Each inputValue In inputValues
Dim dateValue As Date
If DateTime.TryParse(inputValue, dateValue) Then
Console.WriteLine($"'{inputValue}' --> {dateValue:f}")
Else
Console.WriteLine($"Cannot parse '{inputValue}'")
End If
Next
Console.WriteLine("Restored dates...")
End Sub
输出:
Current Time Zone: (UTC-08:00) Pacific Time (US & Canada) The dates on an en-US system: Saturday, June 14, 2014 6:32 AM Thursday, July 10, 2014 11:49 PM Saturday, January 10, 2015 1:16 AM Saturday, December 20, 2014 9:45 PM Monday, June 02, 2014 3:14 PM Saved dates... When restored on an en-GB system, the example displays the following output: Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London The dates on an en-GB system: Cannot parse '6/14/2014 6:32:00 AM' 7/10/2014 11:49:00 PM' --> 07 October 2014 23:49 1/10/2015 1:16:00 AM' --> 01 October 2015 01:16 Cannot parse '12/20/2014 9:45:00 PM' 6/2/2014 3:14:00 PM' --> 06 February 2014 15:14 Restored dates...
示例12: PersistAsInvariantStrings
Public Sub PersistAsInvariantStrings()
SaveDatesAsInvariantStrings()
RestoreDatesAsInvariantStrings()
End Sub
Private Sub SaveDatesAsInvariantStrings()
Dim dates As Date() = {#6/14/2014 6:32AM#, #7/10/2014 11:49PM#,
#1/10/2015 1:16AM#, #12/20/2014 9:45PM#,
#6/2/2014 3:14PM#}
Dim output As String = Nothing
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For ctr As Integer = 0 To dates.Length - 1
Console.WriteLine(dates(ctr).ToString("f"))
output += dates(ctr).ToUniversalTime().ToString("O", CultureInfo.InvariantCulture) +
If(ctr <> dates.Length - 1, "|", "")
Next
Dim sw As New StreamWriter(filenameTxt)
sw.Write(output)
sw.Close()
Console.WriteLine("Saved dates...")
End Sub
Private Sub RestoreDatesAsInvariantStrings()
TimeZoneInfo.ClearCachedData()
Console.WriteLine("Current Time Zone: {0}",
TimeZoneInfo.Local.DisplayName)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Dim sr As New StreamReader(filenameTxt)
Dim inputValues As String() = sr.ReadToEnd().Split({"|"c}, StringSplitOptions.RemoveEmptyEntries)
sr.Close()
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For Each inputValue In inputValues
Dim dateValue As Date
If DateTime.TryParseExact(inputValue, "O", CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind, dateValue) Then
Console.WriteLine($"'{inputValue}' --> {dateValue.ToLocalTime():f}")
Else
Console.WriteLine($"Cannot parse '{inputValue}'")
End If
Next
Console.WriteLine("Restored dates...")
End Sub
输出:
Current Time Zone: (UTC-08:00) Pacific Time (US & Canada) The dates on an en-US system: Saturday, June 14, 2014 6:32 AM Thursday, July 10, 2014 11:49 PM Saturday, January 10, 2015 1:16 AM Saturday, December 20, 2014 9:45 PM Monday, June 02, 2014 3:14 PM Saved dates... When restored on an en-GB system, the example displays the following output: Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London The dates on an en-GB system: 2014-06-14T13:32:00.0000000Z' --> 14 June 2014 14:32 2014-07-11T06:49:00.0000000Z' --> 11 July 2014 07:49 2015-01-10T09:16:00.0000000Z' --> 10 January 2015 09:16 2014-12-21T05:45:00.0000000Z' --> 21 December 2014 05:45 2014-06-02T22:14:00.0000000Z' --> 02 June 2014 23:14 Restored dates...
示例13: PersistAsIntegers
Public Sub PersistAsIntegers()
SaveDatesAsIntegers()
RestoreDatesAsIntegers()
End Sub
Private Sub SaveDatesAsIntegers()
Dim dates As Date() = {#6/14/2014 6:32AM#, #7/10/2014 11:49PM#,
#1/10/2015 1:16AM#, #12/20/2014 9:45PM#,
#6/2/2014 3:14PM#}
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
Dim ticks(dates.Length - 1) As Long
For ctr As Integer = 0 To dates.Length - 1
Console.WriteLine(dates(ctr).ToString("f"))
ticks(ctr) = dates(ctr).ToUniversalTime().Ticks
Next
Dim fs As New FileStream(filenameInts, FileMode.Create)
Dim bw As New BinaryWriter(fs)
bw.Write(ticks.Length)
For Each tick In ticks
bw.Write(tick)
Next
bw.Close()
Console.WriteLine("Saved dates...")
End Sub
Private Sub RestoreDatesAsIntegers()
TimeZoneInfo.ClearCachedData()
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Dim fs As New FileStream(filenameInts, FileMode.Open)
Dim br As New BinaryReader(fs)
Dim items As Integer
Dim dates As DateTime()
Try
items = br.ReadInt32()
ReDim dates(items - 1)
For ctr As Integer = 0 To items - 1
Dim ticks As Long = br.ReadInt64()
dates(ctr) = New DateTime(ticks).ToLocalTime()
Next
Catch e As EndOfStreamException
Console.WriteLine("File corruption detected. Unable to restore data...")
Exit Sub
Catch e As IOException
Console.WriteLine("Unspecified I/O error. Unable to restore data...")
Exit Sub
Catch e As OutOfMemoryException 'Thrown in array initialization.
Console.WriteLine("File corruption detected. Unable to restore data...")
Exit Sub
Finally
br.Close()
End Try
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For Each value In dates
Console.WriteLine(value.ToString("f"))
Next
Console.WriteLine("Restored dates...")
End Sub
输出:
Current Time Zone: (UTC-08:00) Pacific Time (US & Canada) The dates on an en-US system: Saturday, June 14, 2014 6:32 AM Thursday, July 10, 2014 11:49 PM Saturday, January 10, 2015 1:16 AM Saturday, December 20, 2014 9:45 PM Monday, June 02, 2014 3:14 PM Saved dates... When restored on an en-GB system, the example displays the following output: Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London The dates on an en-GB system: 14 June 2014 14:32 11 July 2014 07:49 10 January 2015 09:16 21 December 2014 05:45 02 June 2014 23:14 Restored dates...
示例14: PersistAsXml
Public Sub PersistAsXml()
' Serialize the data.
Dim leapYears As New List(Of DateTime)()
For year As Integer = 2000 To 2100 Step 4
If Date.IsLeapYear(year) Then
leapYears.Add(New Date(year, 2, 29))
End If
Next
Dim dateArray As DateTime() = leapYears.ToArray()
Dim serializer As New XmlSerializer(dateArray.GetType())
Dim sw As TextWriter = New StreamWriter(filenameXml)
Try
serializer.Serialize(sw, dateArray)
Catch e As InvalidOperationException
Console.WriteLine(e.InnerException.Message)
Finally
If sw IsNot Nothing Then sw.Close()
End Try
' Deserialize the data.
Dim deserializedDates As Date()
Using fs As New FileStream(filenameXml, FileMode.Open)
deserializedDates = CType(serializer.Deserialize(fs), Date())
End Using
' Display the dates.
Console.WriteLine($"Leap year days from 2000-2100 on an {Thread.CurrentThread.CurrentCulture.Name} system:")
Dim nItems As Integer
For Each dat In deserializedDates
Console.Write($" {dat:d} ")
nItems += 1
If nItems Mod 5 = 0 Then Console.WriteLine()
Next
End Sub
输出:
Leap year days from 2000-2100 on an en-GB system: 29/02/2000 29/02/2004 29/02/2008 29/02/2012 29/02/2016 29/02/2020 29/02/2024 29/02/2028 29/02/2032 29/02/2036 29/02/2040 29/02/2044 29/02/2048 29/02/2052 29/02/2056 29/02/2060 29/02/2064 29/02/2068 29/02/2072 29/02/2076 29/02/2080 29/02/2084 29/02/2088 29/02/2092 29/02/2096
示例15: PersistBinary
Public Sub PersistBinary()
SaveDatesBinary()
RestoreDatesBinary()
End Sub
Private Sub SaveDatesBinary()
Dim dates As Date() = {#6/14/2014 6:32AM#, #7/10/2014 11:49PM#,
#1/10/2015 1:16AM#, #12/20/2014 9:45PM#,
#6/2/2014 3:14PM#}
Dim fs As New FileStream(filenameBin, FileMode.Create)
Dim bin As New BinaryFormatter()
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Console.WriteLine("The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For ctr As Integer = 0 To dates.Length - 1
Console.WriteLine(dates(ctr).ToString("f"))
dates(ctr) = dates(ctr).ToUniversalTime()
Next
bin.Serialize(fs, dates)
fs.Close()
Console.WriteLine("Saved dates...")
End Sub
Private Sub RestoreDatesBinary()
TimeZoneInfo.ClearCachedData()
Console.WriteLine("Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Dim fs As New FileStream(filenameBin, FileMode.Open)
Dim bin As New BinaryFormatter()
Dim dates As DateTime() = DirectCast(bin.Deserialize(fs), Date())
fs.Close()
Console.WriteLine("The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For Each value In dates
Console.WriteLine(value.ToLocalTime().ToString("f"))
Next
Console.WriteLine("Restored dates...")
End Sub
输出:
Current Time Zone: (UTC-08:00) Pacific Time (US & Canada) The dates on an en-US system: Saturday, June 14, 2014 6:32 AM Thursday, July 10, 2014 11:49 PM Saturday, January 10, 2015 1:16 AM Saturday, December 20, 2014 9:45 PM Monday, June 02, 2014 3:14 PM Saved dates... When restored on an en-GB system, the example displays the following output: Current Time Zone: (UTC-6:00) Central Time (US & Canada) The dates on an en-GB system: 14 June 2014 08:32 11 July 2014 01:49 10 January 2015 03:16 20 December 2014 11:45 02 June 2014 17:14 Restored dates...