(1) Subtract Date from Date return TimeSpan
Private Sub SubtractDateFromDate() ' both subtracts
Dim sdx As String
Dim ts As TimeSpan
Dim Date1 As New Date(2016, 2, 27)
Dim Date2 As New Date(2017, 3, 25)
ts = Date2.Subtract(Date1) ' using .Subtract function
sdx = String.Format("{0:d} .Subtract {1:d} = {2} {3} days", Date2, Date1, ts, ts.Days)
Console.WriteLine(sdx)
ts = Date2 - Date1 ' subtract using - operator
sdx = String.Format("{0,-13:d} - {1,13:d} = {2} {3} days", Date2, Date1, ts, ts.Days)
Console.WriteLine(sdx)
End Sub
Output:
3/25/2017 .Subtract 2/27/2016 = 392.00:00:00 392 days
3/25/2017 - 2/27/2016 = 392.00:00:00 392 days
A Date subtracted from Date yields TimeSpan as a result. Here
as any other code each of the two ways to subtract is shown.
First the .Subtract function and then the - operator. It's
a matter of taste. The choice is yours
(2) TimeSpan subtracted from Date returns Date
Private Sub SubtractTimeSpanFromDate() ' both subtracts
Dim sdx As String
Dim ts As TimeSpan = New TimeSpan(181, 0, 0, 0) ' 181 days, 0 hours, 0 minutes, 0 seconds
Dim Date2 As Date
Dim Date1 As New Date(2016, 12, 17)
Date2 = Date1.Subtract(ts)
sdx = String.Format("{0:d} .Subtract ({1} days & {2} hours) = {3:d}", Date1, ts.Days, ts.Hours, Date2)
Console.WriteLine(sdx)
Date2 = Date1 - ts
sdx = String.Format("{0:d} - ({1} days & {2} hours) = {3:d}", Date1, ts.Days, ts.Hours, Date2)
Console.WriteLine(sdx)
End Sub
Output:
12/17/2016 .Subtract (181 days & 0 hours) = 6/19/2016
12/17/2016 - (181 days & 0 hours) = 6/19/2016
This example show Date minus a TimeSpan yielding another
Date as a result. As usual both the function .Subtract
and the operator - are shown. The result is the same
either way
(3) DateTime.Compare and DateTime.CompareTo
Private Sub DateTimeCompare()
Dim answer1, answer2 As Integer
Dim ansarry = {"less than", "equal to", "greater than"}
Dim sdx As String
Dim dt0 As New Date(2017, 9, 19, 11, 22, 5)
Dim dt1 As New Date(2016, 10, 10, 15, 16, 17)
Dim dt2 As New Date(2022, 11, 15, 15, 27, 27)
answer1 = Compare(dt0, dt1)
answer2 = dt1.CompareTo(dt2)
Console.WriteLine("Compare(dt0, dt1)")
sdx = String.Format("{0} {1} {2}", dt0, ansarry(answer1 + 1), dt1)
Console.WriteLine(sdx)
Console.WriteLine()
Console.WriteLine("dt1.CompareTo(dt2)")
sdx = String.Format("{0} {1} {2}", dt1, ansarry(answer2 + 1), dt2)
Console.WriteLine(sdx)
End Sub
Output:
Compare(dt0, dt1)
9/19/2017 11:22:05 AM greater than 10/10/2016 3:16:17 PM
dt1.CompareTo(dt2)
10/10/2016 3:16:17 PM less than 11/15/2022 3:27:27 PM
DateTime Compare and CompareTo return the same result.
-1 for less than, 0 for equals to, and 1 for greater than.
The difference rest in how they are called. Compare requires
two date/times as inputs. CompareTo requires an instantiated
DateTime object for the first DateTime to compare and a
second DateTime passed in as the one input parameter
object
(4) DateTime.Add function and DateTime + operator
Private Sub DateTimeAdd()
Dim sdx As String
Dim dt0 As New Date(2017, 9, 19, 11, 22, 5)
Dim ts0 As New TimeSpan(135, 12, 15, 33)
Dim ts1 As New TimeSpan(109, 2, 5, 27)
Dim dt1, dt2 As Date
dt1 = dt0.Add(ts0) ' <=====DateTime.Add function=====>
dt2 = dt0 + ts1 ' <=====DateTime + TimeSpan operator=====>
sdx = String.Format("{0} .Add {1} {2}", dt0, ts0, dt1)
Console.WriteLine(sdx)
sdx = String.Format("{0,-24}+ {1} {2}", dt0, ts1, dt2)
Console.WriteLine(sdx)
End Sub
Output:
9/19/2017 11:22:05 AM .Add 135.12:15:33 2/1/2018 11:37:38 PM
9/19/2017 11:22:05 AM + 109.02:05:27 1/6/2018 1:27:32 PM
Here we are adding a date (and optionally time) to
a time span. The returned item is type DateTime.
Both the function .ADD and the operator + are used.
They are equivalent
(5) DateTime: .AddYears, AddMonths, AddDays
Private Sub AddAll()
Dim sdx As String
Dim years = 525
Dim months = 18
Dim days = 27
Dim dt0 = New Date(1492, 10, 22)
Dim dt1, dt2, dt3 As Date
dt1 = dt0.AddYears(years) ' add years
sdx = String.Format("{0:d} + {1,-4} {2,-7} = {3,10:d}", dt0, years, "years", dt1)
Console.WriteLine(sdx)
dt2 = dt1.AddMonths(months) ' add months
sdx = String.Format("{0:d} + {1,-4} {2,-7} = {3,10:d}", dt1, months, "months", dt2)
Console.WriteLine(sdx)
dt3 = dt2.AddDays(days) ' add days
sdx = String.Format("{0,10:d} + {1,-4} {2,-7} = {3,10:d}", dt2, days, "days", dt3)
Console.WriteLine(sdx)
End Sub
Output:
10/22/1492 + 525 years = 10/22/2017
10/22/2017 + 18 months = 4/22/2019
4/22/2019 + 27 days = 5/19/2019
In addition to adding years, months, and days, the
library provides for adding hours, minutes, seconds,
milliseconds and ticks
(6) DateTime constructors
Private Sub DateTimeConstructors()
Dim sdx As String
Dim year = 2016
Dim month = 2
Dim day = 28
Dim hour = 13
Dim minute = 45
Dim second = 30
Dim millisecond = 250
Dim CalendarGregorian As New System.Globalization.GregorianCalendar
Dim dt0 As New Date(year, month, day)
Dim dt1 As New Date(year, month, day, hour, minute, second)
Dim dt2 As New Date(year, month, day, hour, minute, second, millisecond)
sdx = String.Format("dt0 = {0:d}", dt0)
Console.WriteLine(sdx)
sdx = String.Format("dt1 = {0:G}", dt1)
Console.WriteLine(sdx)
' I use the O format since it gives information about
' local and utc as well as milliseconds
sdx = String.Format("dt2 = {0:O}", dt2)
Console.WriteLine(sdx)
Dim dt3 As New Date(year, month, day, hour, minute, second, DateTimeKind.Utc)
Dim dt4 As New Date(year, month, day, hour, minute, second, CalendarGregorian)
Dim dt5 As New Date(year, month, day, hour, minute, second, millisecond, DateTimeKind.Local)
sdx = String.Format("dt5 = {0:O}", dt5)
Console.WriteLine(sdx)
Dim dt6 As New Date(year, month, day, hour, minute, second, millisecond, CalendarGregorian)
Dim dt7 As New Date(year, month, day, hour, minute, second, millisecond, CalendarGregorian, DateTimeKind.Utc)
sdx = String.Format("dt7 = {0:O}", dt7)
Console.WriteLine(sdx)
End Sub
Output:
dt0 = 2/28/2016
dt1 = 2/28/2016 1:45:30 PM
dt2 = 2016-02-28T13:45:30.2500000
dt5 = 2016-02-28T13:45:30.2500000-06:00
dt7 = 2016-02-28T13:45:30.2500000Z
There are many contructors in DateTime.New. They
range from a simple New(year,month,day) to one with
nine input parameters, including local or utc time and
Globalization.Calendar types which make for some
interesting time initializing. Calendar types
are treated in more detail elsewhere.
(7) General System.DateTime: DayOfWeek, DayOfYear,
DaysInMonth, IsDaylightSavingTime, IsLeapYear
Private Sub DateTimeGeneral()
Dim sdx As String
Dim year = 2016
Dim month1 = 11
Dim month2 = 7
Dim day = 29
Dim dlt1, dlt2, lpyr As Boolean
Dim dt0 As New Date(year, month1, day)
Dim dt1 As New Date(year, month2, day)
sdx = String.Format("Day of week {0}. Day of year {1} of {2:d}", dt0.DayOfWeek, dt0.DayOfYear, dt0)
Console.WriteLine(sdx)
sdx = String.Format("Days in month {0}", DaysInMonth(year, month1))
Console.WriteLine(sdx)
dlt1 = dt0.IsDaylightSavingTime
dlt2 = dt1.IsDaylightSavingTime
lpyr = IsLeapYear(year)
sdx = String.Format("Is dt0 month ({0}) on daylight savings time? {1}", dt0.Month, dlt1)
Console.WriteLine(sdx)
sdx = String.Format("Is dt1 month ({0}) on daylight savings time? {1}", dt1.Month, dlt2)
Console.WriteLine(sdx)
sdx = String.Format("Is {0} a leap year? {1}", dt1.Year, lpyr)
Console.WriteLine(sdx)
End Sub
Output:
Day of week Tuesday. Day of year 334 of 11/29/2016
Days in month 30
Is dt0 month (11) on daylight savings time? False
Is dt1 month (7) on daylight savings time? True
Is 2016 a leap year? True
Here is some of the most practical and useful
information given by DateTime: the day of the week, the day of the year,
the number of days in the month, is it daylight savings time?,
is it a leap year? .Net makes it easy for us
(8) DateTime TimeOfDay, Today, ToUniversalTime
Private Sub DateTimeGeneral2()
Dim dt0 As Date
Dim sdx As String
dt0 = Now
sdx = String.Format("Time of day (when program was executed) {0}", dt0.TimeOfDay)
Console.WriteLine(sdx)
sdx = String.Format("Current date (when program was executed) {0:d}", Today)
Console.WriteLine(sdx)
sdx = String.Format("Time UTC (when program was executed) {0:u}", dt0.ToUniversalTime)
Console.WriteLine(sdx)
End Sub
Output:
Time of day (when program was executed) 20:09:31.5166666
Current date (when program was executed) 11/29/2015
Time UTC (when program was executed) 2015-11-30 02:09:31Z
So TimeOfDay is the time of day of the referenced DateTime object.
The current date give by .Today comes not from a referenced object
but the current date in your system. The function ToUniversalTime
calculates universal time i.e. UTC or GMT. Somewhere is the bowels
of your operating system the Universal Time offset is stored. However one
can define a UTC offset also. More elsewhere on that
600
1200
1800
2400
3000
3600
4200
4800
5400
6000
6600
7200
7800
8400
9000
9600
10200
10800
11400
12000
12600
13200
13800
14400
15000
15600
16200
16800
17400
18000
18600
19200
19800
20400