5.1.Date
5.1.1.Dim thirdOfJuly As Date =
#7/3/1776 11:59:59 PM#
Public Class Tester
Public Shared Sub Main
Dim thirdOfJuly As Date = # 7 / 3 / 1776 11 : 59 : 59 PM#
Dim fourthOfJuly As New Date( 1776 , 7 , 4 )
Dim inTheMorning As New Date( 1776 , 7 , 4 , 9 , 45 , 30 )
Console.WriteLine( _
"The 3rd and 4th of July, 1776..." & _
vbNewLine & vbNewLine & _
"#7/3/1776 11:59:59 PM# ... " & _
thirdOfJuly.ToString & vbNewLine & _
"New Date(1776, 7, 4) ... " & _
fourthOfJuly.ToString & vbNewLine & _
"New Date(1776, 7, 4, 9, 45, 30) ... " & _
inTheMorning.ToString)
End Sub
End Class
|
The 3rd and 4th of July, 1776...
#7/3/1776 11:59:59 PM# ... 03/07/1776 11:59:59 PM
New Date(1776, 7, 4) ... 04/07/1776 12:00:00 AM
New Date(1776, 7, 4, 9, 45, 30) ... 04/07/1776 9:45:30 AM
5.1.2.Dim fourthOfJuly As New Date(1776, 7, 4)
Public Class Tester
Public Shared Sub Main
Dim thirdOfJuly As Date = # 7 / 3 / 1776 11 : 59 : 59 PM#
Dim fourthOfJuly As New Date( 1776 , 7 , 4 )
Dim inTheMorning As New Date( 1776 , 7 , 4 , 9 , 45 , 30 )
Console.WriteLine( _
"The 3rd and 4th of July, 1776..." & _
vbNewLine & vbNewLine & _
"#7/3/1776 11:59:59 PM# ... " & _
thirdOfJuly.ToString & vbNewLine & _
"New Date(1776, 7, 4) ... " & _
fourthOfJuly.ToString & vbNewLine & _
"New Date(1776, 7, 4, 9, 45, 30) ... " & _
inTheMorning.ToString)
End Sub
End Class
|
The 3rd and 4th of July, 1776...
#7/3/1776 11:59:59 PM# ... 03/07/1776 11:59:59 PM
New Date(1776, 7, 4) ... 04/07/1776 12:00:00 AM
New Date(1776, 7, 4, 9, 45, 30) ... 04/07/1776 9:45:30 AM
5.1.3.Dim inTheMorning As New Date(1776, 7, 4, 9, 45, 30)
Public Class Tester
Public Shared Sub Main
Dim thirdOfJuly As Date = # 7 / 3 / 1776 11 : 59 : 59 PM#
Dim fourthOfJuly As New Date( 1776 , 7 , 4 )
Dim inTheMorning As New Date( 1776 , 7 , 4 , 9 , 45 , 30 )
Console.WriteLine( _
"The 3rd and 4th of July, 1776..." & _
vbNewLine & vbNewLine & _
"#7/3/1776 11:59:59 PM# ... " & _
thirdOfJuly.ToString & vbNewLine & _
"New Date(1776, 7, 4) ... " & _
fourthOfJuly.ToString & vbNewLine & _
"New Date(1776, 7, 4, 9, 45, 30) ... " & _
inTheMorning.ToString)
End Sub
End Class
|
The 3rd and 4th of July, 1776...
#7/3/1776 11:59:59 PM# ... 03/07/1776 11:59:59 PM
New Date(1776, 7, 4) ... 04/07/1776 12:00:00 AM
New Date(1776, 7, 4, 9, 45, 30) ... 04/07/1776 9:45:30 AM
5.1.4.dateVariable = #1/1/2004#
public class Test
public Shared Sub Main
Dim dteExpiration As Date
dteExpiration = # 1 / 1 / 2004 #
Console.WriteLine(dteExpiration)
End Sub
End class
|
01/01/2004 12:00:00 AM
5.1.5.dateVariable = #8/27/2001 6:29:11 PM#
public class Test
public Shared Sub Main
Dim dteExpiration As Date
dteExpiration = # 8 / 27 / 2001 6 : 29 : 11 PM#
Console.WriteLine(dteExpiration)
End Sub
End class
|
27/08/2001 6:29:11 PM
5.1.6.dateVariable = 'July 2, 2002'
public class Test
public Shared Sub Main
Dim dteExpiration As Date
dteExpiration = "July 2, 2002"
Console.WriteLine(dteExpiration)
End Sub
End class
|
02/07/2002 12:00:00 AM
5.1.7.Add 3 years to a date
public class Test
public Shared Sub Main
Dim Expiration As Date
Dim NewExpiration As Date
Expiration = # 1 / 1 / 2005 #
Expiration.AddYears( 3 )
NewExpiration = Expiration.AddYears( 3 )
Console.WriteLine( "The new expiration date is: " & NewExpiration)
End Sub
End class
|
The new expiration date is: 01/01/2008
5.1.8.Date AddYears, AddMonths and AddDays
Public Class Tester
Public Shared Sub Main
Dim rightNow As Date = Now
Console.WriteLine( "RightNow: " & rightNow.ToString)
Console.WriteLine( "RightNow.AddYears(2): " & rightNow.AddYears( 2 ))
Console.WriteLine( "RightNow.AddMonths(3): " & rightNow.AddMonths( 3 ))
Console.WriteLine( "RightNow.AddDays(4): " & rightNow.AddDays( 4 ))
End Sub
End Class
|
RightNow: 11/05/2007 9:09:20 PM
RightNow.AddYears(2): 11/05/2009 9:09:20 PM
RightNow.AddMonths(3): 11/08/2007 9:09:20 PM
RightNow.AddDays(4): 15/05/2007 9:09:20 PM
5.1.9.Add Time to current time
Public Class Tester
Public Shared Sub Main
Dim rightNow As Date = Now
Console.WriteLine( "RightNow: " & rightNow.ToString)
' - ---- Add time values.
Console.WriteLine( "RightNow.AddHours(5): " & rightNow.AddHours( 5 ))
Console.WriteLine( "RightNow.AddMinutes(6): " & rightNow.AddMinutes( 6 ))
Console.WriteLine( "RightNow.AddSeconds(7): " & rightNow.AddSeconds( 7 ))
Console.WriteLine( "RightNow.AddMilliseconds(8000): " & rightNow.AddMilliseconds( 8000 ))
End Sub
End Class
|
RightNow: 11/05/2007 9:09:20 PM
RightNow.AddHours(5): 12/05/2007 2:09:20 AM
RightNow.AddMinutes(6): 11/05/2007 9:15:20 PM
RightNow.AddSeconds(7): 11/05/2007 9:09:27 PM
RightNow.AddMilliseconds(8000): 11/05/2007 9:09:28 PM
5.1.10.Elapsed Ticks
Public Class Tester
Public Shared Sub Main
Dim ticksBefore As Long
Dim ticksAfter As Long
Dim tickSeconds As Double
ticksBefore = Now.Ticks
MsgBox( "Press OK to see elapsed seconds" )
ticksAfter = Now.Ticks
tickSeconds = (ticksAfter - ticksBefore) / 10000000.0
Console.WriteLine( "Elapsed seconds: " & tickSeconds.ToString())
End Sub
End Class
|
Elapsed seconds: 0.6875
5.1.11.Subtract Time
Public Class Tester
Public Shared Sub Main
Dim results As New System.Text.StringBuilder
Dim rightNow As Date = Now
results.AppendLine( "RightNow: " & rightNow.ToString)
results.AppendLine()
results.AppendLine( "One year ago: " & _
rightNow.AddYears(- 1 ).ToString)
results.AppendLine( "365.25 days ago: " & _
rightNow.AddDays(- 365.25 ).ToString)
Console.WriteLine(results.ToString())
End Sub
End Class
|
RightNow: 11/05/2007 9:09:36 PM
One year ago: 11/05/2006 9:09:36 PM
365.25 days ago: 11/05/2006 3:09:36 PM
5.1.12.Date ToString, ToLongDateString, ToShortDateString,
ToLongTimeString, ToShortTimeString and ToUniversalTime
Public Class Tester
Public Shared Sub Main
Dim rightNow As Date = Now
Dim result As New System.Text.StringBuilder
result.AppendLine( """Now""..." )
result.Append( "ToString: " ).AppendLine(rightNow.ToString)
result.Append( "ToLongDateString: " )
result.AppendLine(rightNow.ToLongDateString)
result.Append( "ToShortDateString: " )
result.AppendLine(rightNow.ToShortDateString)
result.Append( "ToLongTimeString: " )
result.AppendLine(rightNow.ToLongTimeString)
result.Append( "ToShortTimeString: " )
result.AppendLine(rightNow.ToShortTimeString)
result.Append( "ToUniversalTime: " )
result.AppendLine(rightNow.ToUniversalTime)
result.AppendLine()
Console.WriteLine(result.ToString)
End Sub
End Class
|
Now"...
ToString: 11/05/2007 9:09:26 PM
ToLongDateString: May 11, 2007
ToShortDateString: 11/05/2007
ToLongTimeString: 9:09:26 PM
ToShortTimeString: 9:09 PM
ToUniversalTime: 12/05/2007 4:09:26 AM
5.1.13.System Time
Public Class Tester
Public Shared Sub Main
Dim rightNow As Date = Now
Dim result As New System.Text.StringBuilder
result.AppendLine( """Now""..." )
result.AppendLine()
result.Append( "Date: " ).AppendLine(rightNow.ToShortDateString)
result.Append( "Time: " ).AppendLine(rightNow.ToShortTimeString)
result.Append( "Ticks: " ).Append(rightNow.Ticks.ToString)
Console.WriteLine(result.ToString())
End Sub
End Class
|
Now"...
Date: 11/05/2007
Time: 9:09 PM
Ticks: 633145145771093750
5.1.14.Moon Phase
Public Class Tester
Public Shared Sub Main
Dim phaseDay As Double
Dim result As String
phaseDay = MoonPhase(Now.ToUniversalTime)
result = "UTC is now: " & _
Now.ToUniversalTime.ToString( "u" ) & vbNewLine & vbNewLine
If (phaseDay < 0 ) Then
result &= "Approx days until new moon: " & _
(-phaseDay).ToString( "F1" )
Else
result &= "Approx days since new moon: " & _
phaseDay.ToString( "F1" )
End If
Console.WriteLine(result)
End Sub
Public Shared Function MoonPhase(ByVal dateUtc As Date) As Double
Dim days As Double = dateUtc.Subtract(# 1 / 1 / 1600 #).TotalDays
Dim cycles As Double = days * 0.03386319 - 12.5
Return Math.IEEERemainder(cycles, 1.0 ) * 29.53059
End Function
End Class
|
UTC is now: 2007-05-12 04:09:30Z
Approx days until new moon: 4.7
5.1.15.Parse Date: Test an invalid date
Public Class Tester
Public Shared Sub Main
Dim testDate As String
Dim results As New System.Text.StringBuilder
' - ---- Test an invalid date.
testDate = "Febtember 43, 2007"
If (IsDate(testDate) = True) Then _
results.AppendLine(Date.Parse(testDate).ToString)
Console.WriteLine(results.ToString())
End Sub
End Class
|
5.1.16.Date and TimeSpan
public class Test
public Shared Sub Main
Dim date1 As Date = # 7 / 20 / 2004 #
Dim date2 As Date = # 8 / 7 / 2004 #
Dim elapsed_time As TimeSpan
elapsed_time = Date.op_Subtraction(date2, date1)
Console.WriteLine(elapsed_time.Days.ToString)
End Sub
End class
|
18
5.1.17.ToLongDateString
public class Test
public Shared Sub Main
Dim theDate As Date
theDate = # 2 / 13 / 1975 3 : 00 : 00 AM#
Console.WriteLine(theDate.ToLongDateString)
End Sub
End class
|
February 13, 1975
5.1.18.ToLongTimeString
public class Test
public Shared Sub Main
Dim theDate As Date
theDate = # 2 / 13 / 1975 3 : 00 : 00 AM#
Console.WriteLine(theDate.ToLongTimeString)
End Sub
End class
|
3:00:00 AM
5.1.19.Date.ToOADate
public class Test
public Shared Sub Main
Dim D As Date = Now()
Dim F As Double = D.ToOADate()
Console.WriteLine(F)
End Sub
End class
|
39213.5204361921
Ticks since 12:00AM January 1, 1 CE=633144834075000000
5.1.21.Compare Date value in If statement
Module Module1
Sub Main()
If (Now.Hour < 12 ) Then
Console.WriteLine( "Good morning" )
ElseIf (Now.Hour < 18 ) Then
Console.WriteLine( "Good day" )
Else
Console.WriteLine( "Good evening" )
End If
End Sub
End Module
|
Good evening
5.1.22.Select Day Of Week
Module Module1
Sub Main()
Dim DayOfWeek As Integer
DayOfWeek = Now.DayOfWeek
Select Case DayOfWeek
Case 1
Console.WriteLine( "Sunday" )
Case 2
Console.WriteLine( "Monday" )
Case 3
Console.WriteLine( "Tuesday" )
Case 4
Console.WriteLine( "Wednesday" )
Case 5
Console.WriteLine( "Thursday" )
Case 6
Console.WriteLine( "Friday" )
Case 7
Console.WriteLine( "Saturday" )
End Select
End Sub
End Module
|
Thursday
5.2.Date Format
5.2.1.Use format specifiers to control
the date display
Public Class Tester
Public Shared Sub Main
Dim rightNow As Date = Now
Dim result As New System.Text.StringBuilder
result.AppendLine( """Now""..." )
' - ---- Use format specifiers to control the date display.
result.Append( "d: " ).AppendLine(rightNow.ToString( "d" ))
result.Append( "D: " ).AppendLine(rightNow.ToString( "D" ))
result.Append( "t: " ).AppendLine(rightNow.ToString( "t" ))
result.Append( "T: " ).AppendLine(rightNow.ToString( "T" ))
result.Append( "f: " ).AppendLine(rightNow.ToString( "f" ))
result.Append( "F: " ).AppendLine(rightNow.ToString( "F" ))
result.Append( "g: " ).AppendLine(rightNow.ToString( "g" ))
result.Append( "G: " ).AppendLine(rightNow.ToString( "G" ))
result.Append( "M: " ).AppendLine(rightNow.ToString( "M" ))
result.Append( "R: " ).AppendLine(rightNow.ToString( "R" ))
result.Append( "s: " ).AppendLine(rightNow.ToString( "s" ))
result.Append( "u: " ).AppendLine(rightNow.ToString( "u" ))
result.Append( "U: " ).AppendLine(rightNow.ToString( "U" ))
result.Append( "y: " ).AppendLine(rightNow.ToString( "y" ))
Console.WriteLine(result.ToString)
End Sub
End Class
|
Now"...
d: 11/05/2007
D: May 11, 2007
t: 9:09 PM
T: 9:09:27 PM
f: May 11, 2007 9:09 PM
F: May 11, 2007 9:09:27 PM
g: 11/05/2007 9:09 PM
G: 11/05/2007 9:09:27 PM
M: May 11
R: Fri, 11 May 2007 21:09:27 GMT
s: 2007-05-11T21:09:27
u: 2007-05-11 21:09:27Z
U: May 12, 2007 4:09:27 AM
y: May, 2007
5.2.2.Date format: dd
Public Class Tester
Public Shared Sub Main
Dim rightNow As Date = Now
Dim result As New System.Text.StringBuilder
result.Append( "dd: " ).AppendLine(rightNow.ToString( "dd" ))
Console.WriteLine(result.ToString)
End Sub
End Class
|
dd: 11
5.2.3.Date format: ddd
Public Class Tester
Public Shared Sub Main
Dim rightNow As Date = Now
Dim result As New System.Text.StringBuilder
result.Append( "ddd: " ).AppendLine(rightNow.ToString( "ddd" ))
Console.WriteLine(result.ToString)
End Sub
End Class
|
ddd: Fri
5.2.4.Date format: dddd
Public Class Tester
Public Shared Sub Main
Dim rightNow As Date = Now
Dim result As New System.Text.StringBuilder
result.Append( "dddd: " ).AppendLine(rightNow.ToString( "dddd" ))
Console.WriteLine(result.ToString)
End Sub
End Class
|
dddd: Friday
5.2.5.Date format: HH:mm:ss.fff z
Public Class Tester
Public Shared Sub Main
Dim rightNow As Date = Now
Dim result As New System.Text.StringBuilder
result.Append( "HH:mm:ss.fff z: " )
result.AppendLine(rightNow.ToString( "HH:mm:ss.fff z" ))
Console.WriteLine(result.ToString)
End Sub
End Class
|
HH:mm:ss.fff z: 21:09:29.265 -7
5.2.6.Date format: yy/MM/dd g
Public Class Tester
Public Shared Sub Main
Dim rightNow As Date = Now
Dim result As New System.Text.StringBuilder
result.Append( "yy/MM/dd g: " )
result.AppendLine(rightNow.ToString( "yy/MM/dd g" ))
Console.WriteLine(result.ToString)
End Sub
End Class
|
yy/MM/dd g: 07/05/11 A.D.
5.2.7.Date format: s, u, Z
Public Class Tester
Public Shared Sub Main
Dim rightNow As Date = Now.ToUniversalTime
Dim format1 As String = rightNow.ToString( "s" )
Dim format2 As String = rightNow.ToString( "u" )
Dim format3 As String = rightNow.ToString( "s" ) & "Z"
Dim format4 As String = rightNow.ToString( _
"u" ).Substring( 0 , 19 )
Console.WriteLine(String.Format( _
"s: {1}{0}u: {2}{0}T&Z: {3}{0}Neither: {4}" , _
vbNewLine, format1, format2, format3, format4))
End Sub
End Class
|
s: 2007-05-12T04:09:30
u: 2007-05-12 04:09:30Z
T&Z: 2007-05-12T04:09:30Z
Neither: 2007-05-12 04:09:30
5.2.8.Date format: {0:dddd},
Public Class Tester
Public Shared Sub Main
Dim rightNow As Date = Now
Dim weekDay As Integer = rightNow.DayOfWeek
Dim weekDayShort As String = Format(rightNow, "ddd" )
Dim weekDayLong As String = String.Format( "{0:dddd}" , rightNow)
Dim results As String = String.Format( _
"Today's day of the week: {0}, or {1}, or {2}" , _
weekDay, weekDayShort, weekDayLong)
Console.WriteLine(results)
End Sub
End Class
|
Today's day of the week: 5, or Fri, or Friday
5.2.9.Date format: {0:d} ;{1:D}
Public Class Tester
Public Shared Sub Main
Console.WriteLine(DateTime.Now.ToString)
Console.WriteLine(String.Format( "{0:d} ;{1:D} " , DateTime.Now, DateTime.Now))
End Sub
End Class
|
11/05/2007 9:23:30 PM
11/05/2007 ;May 11, 2007
5.2.10.Date format: {0:t} ;{1:T}
Public Class Tester
Public Shared Sub Main
Console.WriteLine(DateTime.Now.ToString)
Console.WriteLine(String.Format( "{0:t} ;{1:T} " , DateTime.Now, DateTime.Now))
End Sub
End Class
|
11/05/2007 9:23:31 PM
9:23 PM ;9:23:31 PM
5.2.11.Date format: {0:f} ;{1:F}
Public Class Tester
Public Shared Sub Main
Console.WriteLine(DateTime.Now.ToString)
Console.WriteLine(String.Format( "{0:f} ;{1:F} " , DateTime.Now, DateTime.Now))
End Sub
End Class
|
11/05/2007 9:23:31 PM
May 11, 2007 9:23 PM ;May 11, 2007 9:23:31 PM
5.2.12.Date format: {0:g} ;{1:G}
Public Class Tester
Public Shared Sub Main
Console.WriteLine(DateTime.Now.ToString)
Console.WriteLine(String.Format( "{0:g} ;{1:G} " , DateTime.Now, DateTime.Now))
End Sub
End Class
|
11/05/2007 9:23:31 PM
11/05/2007 9:23 PM ;11/05/2007 9:23:31 PM
5.2.13.Date format: {0:m} ;{1:M}
Public Class Tester
Public Shared Sub Main
Console.WriteLine(DateTime.Now.ToString)
Console.WriteLine(String.Format( "{0:m} ;{1:M} " , DateTime.Now, DateTime.Now))
End Sub
End Class
|
11/05/2007 9:23:32 PM
May 11 ;May 11
5.2.14.Date format: {0:yyyy/MM/dd HH:mm:ss}
Public Class Tester
Public Shared Sub Main
Console.WriteLine(DateTime.Now.ToString)
Console.WriteLine(String.Format( "{0:yyyy/MM/dd HH:mm:ss}" , DateTime.Now))
End Sub
End Class
|
11/05/2007 9:23:32 PM
2007/05/11 21:23:32
5.2.15.Date format: {0:MMMM-dd-yy HH:mm:ss}
Public Class Tester
Public Shared Sub Main
Console.WriteLine(DateTime.Now.ToString)
Console.WriteLine(String.Format( "{0:MMMM-dd-yy HH:mm:ss}" , DateTime.Now))
End Sub
End Class
|
11/05/2007 9:23:33 PM
May-11-07 21:23:33
5.2.16.Format the current date in various ways
Imports System
Imports Microsoft.VisualBasic
Class Sample
Public Shared Sub Main()
Dim thisDate As DateTime = DateTime.Now
Console.WriteLine( "Standard DateTime Format Specifiers" )
Console.WriteLine( "(d) Short date: . . . . . . . {0:d}" & vbCrLf & _
"(D) Long date:. . . . . . . . {0:D}" & vbCrLf & _
"(t) Short time: . . . . . . . {0:t}" & vbCrLf & _
"(T) Long time:. . . . . . . . {0:T}" & vbCrLf & _
"(f) Full date/short time: . . {0:f}" & vbCrLf & _
"(F) Full date/long time:. . . {0:F}" & vbCrLf & _
"(g) General date/short time:. {0:g}" & vbCrLf & _
"(G) General date/long time: . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(M) Month:. . . . . . . . . . {0:M}" & vbCrLf & _
"(R) RFC1123:. . . . . . . . . {0:R}" & vbCrLf & _
"(s) Sortable: . . . . . . . . {0:s}" & vbCrLf & _
"(u) Universal sortable: . . . {0:u} (invariant)" & vbCrLf & _
"(U) Universal full date/time: {0:U}" & vbCrLf & _
"(Y) Year: . . . . . . . . . . {0:Y}" & vbCrLf, _
thisDate)
End Sub
End Class
|
5.2.17.Date and time and numeric values
using several different cultures.
Imports System.Globalization
Module Example
Public Sub Main()
Dim cultureNames() As String = { "en-US" , "fr-FR" , "de-DE" , "es-ES" }
Dim dateToDisplay As Date = # 9 / 1 / 2009 6 : 32 PM#
Dim value As Double = 9164.32
For Each cultureName As String In cultureNames
Dim culture As New CultureInfo(cultureName)
Console.WriteLine(culture)
Console.WriteLine(culture.Name)
Console.WriteLine(dateToDisplay)
Console.WriteLine(value)
Next
End Sub
End Module
|
5.2.18.Custom Format Strings
Public Module Example
Public Sub Main()
Dim date1 As Date = # 09 / 08 / 2009 #
Console.WriteLine(date1.ToString( "%M" )) ' D isplays 9
End Sub
End Module
|
5.2.19.Defines a custom format string
that displays the day of the week in parentheses after the month
name, day, and year.
Public Module Example
Public Sub Main()
Dim customFormat As String = "MMMM dd, yyyy (dddd)"
Dim date1 As Date = # 8 / 28 / 2009 #
Console.WriteLine(date1.ToString(customFormat))
End Sub
End Module
|
5.3.Date Functions
5.3.1.DateAdd: add two date together
Option Strict On
Public Module DateAddTest
Public Sub Main()
Console.WriteLine(DateAdd(DateInterval.Hour, 36 , Date.Now))
Console.WriteLine(DateAdd(DateInterval.Hour, - 36 , Date.Now))
End Sub
End Module
|
13/05/2007 9:17:01 AM
10/05/2007 9:17:01 AM
5.3.2.Weekday Function
Option Strict On
Public Module WeekdayFunction
Public Sub Main
Dim holiday As Date = # 01 / 01 / 2006 #
If Weekday(holiday, FirstDayOfWeek.Monday) <= 5 Then
Console.WriteLine( "The holiday on {0}, is during the work week." , _
FormatDateTime(holiday, DateFormat.LongDate))
Else
Console.WriteLine( "The holiday on {0}, occurs during the weekend." , _
FormatDateTime(holiday, DateFormat.LongDate))
End If
End Sub
End Module
|
The holiday on January 1, 2006, occurs during the weekend.
5.3.3.Call to WeekdayName combined with call to Weekday
Option Strict On
Imports System.Globalization
Public Module WeekdayNameFunction
Public Sub Main()
Dim birthday As Date = # 07 / 28 / 1993 #
Console.WriteLine(WeekdayName(Weekday(birthday), True))
End Sub
End Module
|
Wed
5.3.4.Determining the weekday of a date
Option Strict On
Imports System.Globalization
Public Module WeekdayNameFunction
Public Sub Main()
'
Dim historicalDate As New DateTime( 1905 , 1 , 9 , New JulianCalendar())
Dim dayOfWeek As Integer = Weekday(historicalDate, FirstDayOfWeek.System)
Console.WriteLine(WeekdayName(dayOfWeek, False, FirstDayOfWeek.System))
End Sub
End Module
|
Sunday
5.3.5.CDate: convert string(8/1/86) to date
Module Tester
Public Sub Main()
Dim sDate As String = "8/1/86"
Dim dtDate As Date = CDate(sDate)
Console.WriteLine( "Date now holds {0}" , dtDate)
End Sub
End Module
|
Date now holds 08/01/1986 12:00:00 AM
5.3.6.CDate: convert string(Tuesday, September 16, 1845 6:45pm) to
Date
Module Tester
Public Sub Main()
Dim sDate As String = "8/1/86"
Dim dtDate As Date = CDate(sDate)
sDate = "Tuesday, September 16, 1845 6:45pm"
dtDate = CDate(sDate)
Console.WriteLine( "Date now holds {0}" , dtDate)
End Sub
End Module
|
Date now holds 16/09/1845 6:45:00 PM
5.3.7.CDate: convert string(15 July, 1215) to Date
Module Tester
Public Sub Main()
Dim sDate As String = "8/1/86"
Dim dtDate As Date = CDate(sDate)
sDate = "15 July, 1215"
dtDate = CDate(sDate)
Console.WriteLine( "Date now holds {0}" , dtDate)
End Sub
End Module
|
Date now holds 15/07/1215 12:00:00 AM
5.3.8.Assign Now() to a date variable
public class Test
public Shared Sub Main
Dim dteExpiration As Date
dteExpiration = Now()
Console.WriteLine(dteExpiration)
End Sub
End class
|
11/05/2007 12:30:59 PM
public class
Test
public Shared Sub
Main
Dim
dteExpiration
As
Date
Dim
lngDays
As
Long
lngDays = DateDiff(DateInterval.Day, #
12
/
31
/
2000
#, Now())
Console.WriteLine(lngDays)
End Sub
End class
5.3.10.TimeOfDay
public class Test
public Shared Sub Main
Console.WriteLine(TimeOfDay)
End Sub
End Class
|
01/01/0001 12:22:14 PM
5.3.11.Calls GetDaysInMonth for the second month in each of five
years in each era.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Public Class SamplesGregorianCalendar
Public Shared Sub Main()
Dim myCal As New GregorianCalendar()
Console.Write( "YEAR" + ControlChars.Tab)
Dim y As Integer
For y = 2001 To 2005
Console.Write(ControlChars.Tab + "{0}" , y)
Next y
Console.Write( "CurrentEra:" )
For y = 2001 To 2005
Console.Write(ControlChars.Tab + "{0}" , myCal.GetDaysInMonth(y, 2 , GregorianCalendar.CurrentEra))
Next y
Console.WriteLine()
Dim i As Integer
For i = 0 To myCal.Eras.Length - 1
Console.Write( "Era {0}:" + ControlChars.Tab, myCal.Eras(i))
For y = 2001 To 2005
Console.Write(ControlChars.Tab + "{0}" , myCal.GetDaysInMonth(y, 2 , myCal.Eras(i)))
Next y
Console.WriteLine()
Next i
End Sub
End Class
|
5.3.12.Call IsLeapMonth() for all the
months
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Public Class MainClass
Public Shared Sub Main()
Dim myCal As New KoreanCalendar()
Dim iMonthsInYear As Integer
Dim y As Integer
For y = 1 To 4338
iMonthsInYear = myCal.GetMonthsInYear(y, KoreanCalendar.CurrentEra)
Dim m As Integer
For m = 1 To iMonthsInYear
Console.Write(ControlChars.Tab + "{0}" , myCal.IsLeapMonth(y, m, KoreanCalendar.CurrentEra))
Next m
Next y
End Sub
End Class
|
5.3.13.The FirstDayOfWeek used for the
en-US culture
Imports System
Imports System.Globalization
Public Class SamplesCalendar
Public Shared Sub Main()
Dim myCI As New CultureInfo( "en-US" )
Dim myCal As Calendar = myCI.Calendar
Dim myCWR As CalendarWeekRule = myCI.DateTimeFormat.CalendarWeekRule
Dim myFirstDOW As DayOfWeek = myCI.DateTimeFormat.FirstDayOfWeek
Console.WriteLine( "The FirstDayOfWeek used for the en-US culture is {0}." , myFirstDOW)
End Sub
End Class
|
5.3.14.Current week in current year
Imports System
Imports System.Globalization
Public Class SamplesCalendar
Public Shared Sub Main()
Dim myCI As New CultureInfo( "en-US" )
Dim myCal As Calendar = myCI.Calendar
Dim myCWR As CalendarWeekRule = myCI.DateTimeFormat.CalendarWeekRule
Dim myFirstDOW As DayOfWeek = myCI.DateTimeFormat.FirstDayOfWeek
Console.WriteLine( "Current week is Week {0} of the current year." , myCal.GetWeekOfYear(DateTime.Now, myCWR, myFirstDOW))
End Sub
End Class
|
5.3.15.Call IsLeapMonth for all the
months in five years in the current era.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Public Class SamplesJulianCalendar
Public Shared Sub Main()
Dim myCal As New JulianCalendar()
Dim iMonthsInYear As Integer
Dim y As Integer
For y = 2001 To 2005
iMonthsInYear = myCal.GetMonthsInYear(y, JulianCalendar.CurrentEra)
Dim m As Integer
For m = 1 To iMonthsInYear
Console.Write(myCal.IsLeapMonth(y, m, JulianCalendar.CurrentEra))
Next m
Next y
End Sub
End Class
|
5.4.Date Parse
5.4.1.Parse Date
Public Class Tester
Public Shared Sub Main
Dim dateParse As Date = Date.Parse( "December 25, 2007" )
Console.WriteLine(dateParse)
End Sub
End Class
|
25/12/2007 12:00:00 AM
5.4.2.Parse Date: Feb 3, 2007
Public Class Tester
Public Shared Sub Main
Dim testDate As String
Dim results As New System.Text.StringBuilder
testDate = "Feb 3, 2007"
If (IsDate(testDate) = True) Then _
results.AppendLine(Date.Parse(testDate).ToString)
Console.WriteLine(results.ToString())
End Sub
End Class
|
03/02/2007 12:00:00 AM
5.4.3.Parse Date: 23:57:58
Public Class Tester
Public Shared Sub Main
Dim testDate As String
Dim results As New System.Text.StringBuilder
' - ---- Test a time.
testDate = "23:57:58"
If (IsDate(testDate) = True) Then _
results.AppendLine(Date.Parse(testDate).ToString)
Console.WriteLine(results.ToString())
End Sub
End Class
|
11/05/2007 11:57:58 PM
5.4.4.Parse Date: December 7, 2007
Public Class Tester
Public Shared Sub Main
Dim testDate As String
Dim results As New System.Text.StringBuilder
' - ---- Test a date.
testDate = "December 7, 2007"
If (IsDate(testDate) = True) Then _
results.AppendLine(Date.Parse(testDate).ToString)
Console.WriteLine(results.ToString())
End Sub
End Class
|
07/12/2007 12:00:00 AM
5.4.5.Parse Date: 2007-07-04T23:59:59
Public Class Tester
Public Shared Sub Main
Dim testDate As String
Dim results As New System.Text.StringBuilder
' - ---- Test a standardized date and time.
testDate = "2007-07-04T23:59:59"
If (IsDate(testDate) = True) Then _
results.AppendLine(Date.Parse(testDate).ToString)
Console.WriteLine(results.ToString())
End Sub
End Class
|
04/07/2007 11:59:59 PM
5.4.6.Parse Date: 2007-07-04T23:59:59Z
Public Class Tester
Public Shared Sub Main
Dim testDate As String
Dim results As New System.Text.StringBuilder
' - ---- Test another standardized UTC date and time.
testDate = "2007-07-04T23:59:59Z"
If (IsDate(testDate) = True) Then _
results.AppendLine(Date.Parse(testDate).ToString)
Console.WriteLine(results.ToString())
End Sub
End Class
|
04/07/2007 4:59:59 PM
5.4.7.Use Parse method to convert a string into a DateTime
Public Class Example
Public Shared Sub Main()
Dim MyString As String = "Jan 1, 2009"
Dim MyDateTime As DateTime = DateTime.Parse(MyString)
Console.WriteLine(MyDateTime)
End Sub
End Class
|
5.5.DateTime
5.5.1.Get Date from long integer
public class Test
public Shared Sub Main
Dim D As New DateTime( 10000333333333333 )
Console.WriteLine( "10000 ticks is " + D.Date())
End Sub
End class
|
10000 ticks is 09/09/0032
5.5.2.Get UniversalTime from DateTime
public class Test
public Shared Sub Main
Dim D As DateTime
D = Now()
Console.WriteLine(D.ToUniversalTime().GetType().Name)
Console.WriteLine( "UTC-LocalTime=" + D.ToUniversalTime().Subtract(D).ToString())
Console.WriteLine( "Ticks since 12:00AM January 1, 1 CE=" + Now().Ticks().ToString())
End Sub
End class
|
DateTime
UTC-LocalTime=07:00:00
Ticks since 12:00AM January 1, 1 CE=633144833635000000
5.5.3.DateTime.Parse(12:00:00 PM)
public class Test
public Shared Sub Main
Console.WriteLine(DateTime.Parse( "12:00:00 PM" ))
End Sub
End class
|
11/05/2007 12:00:00 PM
5.5.4.DateTime parse date and time by DateTimeFormatInfo
public class Test
public Shared Sub Main
Dim Provider As New System.Globalization.DateTimeFormatInfo()
Console.WriteLine(Provider.AMDesignator())
Console.WriteLine(DateTime.ParseExact( "12:42" , "hh:mm" , Provider))
End Sub
End class
|
AM
11/05/2007 12:42:00 AM
5.5.4.DateTime parse date and time by DateTimeFormatInfo
public class Test
public Shared Sub Main
Dim Provider As New System.Globalization.DateTimeFormatInfo()
Console.WriteLine(Provider.AMDesignator())
Console.WriteLine(DateTime.ParseExact( "12:42" , "hh:mm" , Provider))
End Sub
End class
|
AM
11/05/2007 12:42:00 AM
5.5.6.Get Hour from DateTime
Imports System
Public Class Greeting
Shared Sub Main()
Dim dtCurrent As System.DateTime
Dim iHour As Integer
dtCurrent = dtCurrent.Now()
iHour = dtCurrent.Hour
If (iHour < 12 ) Then
Console.Writeline( "Good Morning!" )
End If
If (iHour >= 12 ) And (iHour < 18 ) Then
Console.WriteLine( "Good Afternoon!" )
End If
If (iHour >= 18 ) Then
Console.WriteLine( "Good Evening!" )
End If
End Sub
End Class
|
Good Afternoon!
5.5.7.Timing code block
public class Test
public Shared Sub Main
Const TRIALS As Integer = 10000000
Dim start_time As DateTime
Dim stop_time As DateTime
Dim elapsed_time As TimeSpan
Dim i As Integer
start_time = Now
For i = 1 To TRIALS
Next i
stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
Console.WriteLine(elapsed_time.TotalSeconds.ToString( "0.000000" ))
start_time = Now
For i = 1 To TRIALS
Next i
stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
Console.WriteLine(elapsed_time.TotalSeconds.ToString( "0.000000" ))
End Sub
End class
|
0.015625
0.015625
5.5.8.Displays the total number of weeks in the current year.
Imports System
Imports System.Globalization
Public Class SamplesCalendar
Public Shared Sub Main()
Dim myCI As New CultureInfo( "en-US" )
Dim myCal As Calendar = myCI.Calendar
Dim myCWR As CalendarWeekRule = myCI.DateTimeFormat.CalendarWeekRule
Dim myFirstDOW As DayOfWeek = myCI.DateTimeFormat.FirstDayOfWeek
Dim LastDay = New System.DateTime(DateTime.Now.Year, 12 , 31 )
Console.WriteLine( "There are {0} weeks in the current year ({1})." , myCal.GetWeekOfYear(LastDay, myCWR, myFirstDOW), LastDay.Year)
End Sub
End Class
|
5.6.DateTimeFormatInfo
5.6.1.AM Designator
public class Test
public Shared Sub Main
Dim Provider As New System.Globalization.DateTimeFormatInfo()
Console.WriteLine(Provider.AMDesignator())
Console.WriteLine(DateTime.ParseExact( "12:42" , "hh:mm" , Provider))
End Sub
End class
|
AM
11/05/2007 12:42:00 AM
5.7.Now
5.7.1.Current time
Public Class Tester
Public Shared Sub Main
Console.WriteLine(Now.ToLongTimeString)
End Sub
End Class
|
9:07:41 PM
5.7.2.Now.Ticks
Public Class Tester
Public Shared Sub Main
Dim lastTicks As Long
Dim numTicks As Long
Dim endTime As Date
Dim results As String
endTime = Now.AddSeconds( 1 )
Do
If (Now.Ticks <> lastTicks) Then
numTicks += 1
lastTicks = Now.Ticks
End If
Loop Until (Now > endTime)
results = "Now.Ticks: " & Now.Ticks.ToString & vbNewLine & _
"Number of updates per second: " & numTicks.ToString
Console.WriteLine(results)
End Sub
End Class
|
Now.Ticks: 633145145786406250
Number of updates per second: 65
5.7.3.Different parts of Now
Public Class Tester
Public Shared Sub Main
Dim rightNow As Date = Now
Dim hourNow As Integer = rightNow.Hour
Dim minuteNow As Integer = rightNow.Minute
Dim secondNow As Integer = rightNow.Second
Dim millisecondNow As Integer = rightNow.Millisecond
Dim results As String = String.Format( _
"Hour: {1}{0}Minute: {2}{0}Second: " & _
"{3}{0}Millisecond: {4}" , vbNewLine, _
hourNow, minuteNow, secondNow, millisecondNow)
Console.WriteLine(results)
End Sub
End Class
|
Hour: 21
Minute: 9
Second: 39
Millisecond: 640
5.8.Today
5.8.1.Today
Module Tester
Public Sub Main()
Dim dtNow As Date = Now
dtNow = Today
Console.WriteLine( "Current date: {0}" , dtNow)
End Sub
End Module
|
Current date: 11/05/2007 12:00:00 AM
5.9.TimeSpan
5.9.1.Add TimeSpan to Date
Option Strict On
Public Class DateMembers
Public Shared Sub Main()
Dim dat As Date = New Date( 2000 , 1 , 1 )
Dim ts As New TimeSpan( 30 , 0 , 0 , 0 )
Console.WriteLine( "The original date is " & dat)
dat = dat.AddMonths( 5 )
Console.WriteLine( "The new date is " & dat)
dat = dat + ts
Console.WriteLine( "The new date is " & dat)
End Sub
End Class
|
The original date is 01/01/2000
The new date is 01/06/2000
The new date is 01/07/2000
5.9.2.TimeSpan and Date Substract
Public Class Tester
Public Shared Sub Main
Dim birthDay As Date
Dim lifeTime As TimeSpan
Dim lifeDays As Integer
birthDay = Date.Parse( "December 25, 2007" )
lifeTime = Now.Subtract(birthDay)
lifeDays = lifeTime.Days
Console.WriteLine(String.Format( _
"There are {0} days between {1:D} and {2:D}" , _
lifeDays, birthDay, Now))
End Sub
End Class
|
There are -227 days between December 25, 2007 and May 11, 2007
5.9.3.Date different parts
Public Class Tester
Public Shared Sub Main
Dim rightNow As Date = Now
Dim yearNow As Integer = rightNow.Year
Dim monthNow As Integer = rightNow.Month
Dim dayNow As Integer = rightNow.Day
Dim results As String = String.Format( _
"Year: {1}{0}Month: {2}{0}Day: {3}{0}" , _
vbNewLine, yearNow, monthNow, dayNow)
Console.WriteLine(results)
End Sub
End Class
|
Year: 2007
Month: 5
Day: 11
5.9.4.Day of the week
Public Class Tester
Public Shared Sub Main
Console.WriteLine(Today.DayOfWeek.ToString())
End Sub
End Class
|
Friday
5.9.5.Ticks times value
public class Test
public Shared Sub Main
Dim ts1, ts2 As TimeSpan
ts1 = New TimeSpan( 1 , 2 , 3 )
ts2 = New TimeSpan(ts1.Ticks * 12 )
Console.WriteLine(ts1.ToString)
Console.WriteLine(ts2.ToString)
End Sub
End class
|
01:02:03
12:24:36
5.10.TimeZone
5.10.1.Time zone: DateTimeKind.Local,
DateTimeKind.Utc, DateTimeKind.Unspecified
Public Class Tester
Public Shared Sub Main
Dim theZone As TimeZone = TimeZone.CurrentTimeZone
Dim result As New System.Text.StringBuilder
result.Append( "DaylightName: " ).AppendLine( _
theZone.DaylightName)
result.Append( "StandardName: " ).AppendLine( _
theZone.StandardName)
result.Append( "IsDaylightSavingTime(Now): " ).AppendLine( _
theZone.IsDaylightSavingTime(Now))
result.Append( "GetUtcOffset(Now): " ).AppendLine( _
theZone.GetUtcOffset(Now).ToString())
result.Append( "System time is Local Time: " )
result.AppendLine(Now.Kind = DateTimeKind.Local)
result.Append( "System time is Universal Coordinated Time: " )
result.AppendLine(Now.Kind = DateTimeKind.Utc)
result.Append( "System time is Unspecified: " )
result.AppendLine(Now.Kind = DateTimeKind.Unspecified)
Console.WriteLine(result.ToString())
End Sub
End Class
|
DaylightName: Pacific Daylight Time
StandardName: Pacific Standard Time
IsDaylightSavingTime(Now): True
GetUtcOffset(Now): -07:00:00
System time is Local Time: True
System time is Universal Coordinated Time: False
System time is Unspecified: False
5.11.Stopwatch
5.11.1.Use StopWatch
Public Class Tester
Public Shared Sub Main
Dim testWatch As New System.Diagnostics.Stopwatch
Dim results As String
' - ---- Start counting.
testWatch.Start()
MsgBox( "Press OK to see elapsed seconds" )
' - ---- Stop and record.
results = String.Format( _
"testWatch.Elapsed.Seconds: {0}{3}" & _
"testWatch.Elapsed.TotalSeconds: {1}{3}" & _
"testWatch.ElapsedMilliseconds / 1000: {2}" , _
testWatch.Elapsed.Seconds, _
testWatch.Elapsed.TotalSeconds, _
testWatch.ElapsedMilliseconds / 1000 , vbNewLine)
Console.WriteLine(results)
End Sub
End Class
|
testWatch.Elapsed.Seconds: 2
testWatch.Elapsed.TotalSeconds: 2.2114405
testWatch.ElapsedMilliseconds / 1000: 2.211
5.11.2.StopWatch: ElapsedMilliseconds
Public Class Tester
Public Shared Sub Main
Dim lastMillis As Long
Dim numMillis As Long
Dim testWatch As New System.Diagnostics.Stopwatch
Dim endTime As Date
Dim results As String
endTime = Now.AddSeconds( 1 )
testWatch.Start()
Do
If (testWatch.ElapsedMilliseconds <> lastMillis) Then
numMillis += 1
lastMillis = testWatch.ElapsedMilliseconds
End If
Loop Until (Now > endTime)
results = "Elapsed milliseconds: " & _
testWatch.ElapsedMilliseconds.ToString & vbNewLine & _
"Number of updates per second: " & numMillis.ToString
Console.WriteLine(results)
End Sub
End Class
|
Elapsed milliseconds: 1001
Number of updates per second: 1001
Wyszukiwarka
Podobne podstrony:
kurs vba Date Time 5 kolumnyEXCEL 2003 Kurs VBAkurs vba Language Basics 1 kolumnykurs vba Data Type 2 kolumnykurs vba Statements 4 kolumnykurs vba Operator 3 kolumnyDate time routineskurs vba Data Type 2kurs vba Statements 4kurs vba Operator 3Stephanie Rowe Manhattan Otherworld 1 Date Me, Baby, One More TimeKURS ETYKIChoroba hemolityczna p odu na kurszapotrzebowanie ustroju na skladniki odzywcze 12 01 2009 kurs dla pielegniarek (2)kurswady postawy kurswięcej podobnych podstron