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
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
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
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
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
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
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
dateVariable = #4#
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
dateVariable = #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
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
Dim fourthOfJuly As New Date( 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
Dim inTheMorning As New Date( 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
Dim thirdOfJuly As Date = #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
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
Get Ticks from DateTime
public class Test
public Shared Sub Main
Console.WriteLine("Ticks since 12:00AM January 1, 1 CE="+ Now().Ticks().ToString())
End Sub
End class
Ticks since 12:00AM January 1, 1 CE=633144834075000000
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
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
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
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
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
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
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