Using DateTimeOffset
Private Sub DtoBasic()
   Dim year = 2016
   Dim month = 1
   Dim day = 1
   Dim gmtoffset = -4
   Dim dt1 As New Date(year, month, day)
   Dim dt2 As New Date(year, month, day, 0, 0, 0)
   Dim tsm4 As New TimeSpan(gmtoffset, 0, 0)
   ' create DateTimeOffset with Date dt1
   Dim dto1 = New System.DateTimeOffset(dt1)
   ' create DateTimeOffset with Date dt1 and offset -4 hours
   Dim dto2 = New System.DateTimeOffset(dt2, tsm4)
   Console.WriteLine(" Date dt1  = {0}", dt1)
   Console.WriteLine(" DateTimeOffset dto1 = {0} ", dto1)
   Console.WriteLine(" DateTimeOffset dto2 = {0} ", dto2)
End Sub
 
Output:

  ' Date dt1  = 1/1/2016 12:00:00 AM
  ' DateTimeOffset dto1 = 1/1/2016 12:00:00 AM -06:00 
  ' DateTimeOffset dto2 = 1/1/2016 12:00:00 AM -04:00 
DateTimeOffset is preferred over DateTime. With DateTimeOffset there is always an offset from UTC (Universal Coordinated Time), also known as GMT. A simple Date/DateTime can be ambigous because it could be local, based on your own operating system time, having the offset from UTC of your location or it could be for some other offset from GMT. DateTimeOffset removes the guesswork, since it is specific as far as the offset from UTC. Above I created a DateTimeOffset using a simple date. Since I did not specify an offset, it used my local offset, -6 hours. The other DateTimeOffset used the 2 parameter constructor that takes a Date and a TimeSpan. The TimeSpan can be initiated with with any desired offset from GMT. Any adding or subtracting with DateTimeOffset will always compensate for the offset from GMT/UTC
DateTimeOffset Constructors examples
 Private Sub DTOffsetConstructors()
    Dim dto1, dto2, dto3, dto4 As DateTimeOffset
    Dim sdx As String
    Dim year = 2020 : Dim month = 6 : Dim day = 15
    Dim hour = 0 : Dim minute = 30 : Dim seconds = 0 : Dim milliseconds = 100
    Dim ts1, ts2, ts3 As TimeSpan
    ts1 = New TimeSpan(10, 0, 0)
    ts2 = New TimeSpan(-10, 0, 0)
    ts3 = New TimeSpan(-6, 0, 0)
    Dim cal As New JulianCalendar
    Dim dt1 As New Date(year, month, day)
    dto1 = New DateTimeOffset(dt1)
    sdx = String.Format(" dto1 = {0}", dto1)
    Console.WriteLine(sdx)
    dto2 = New DateTimeOffset(dt1, ts1)
    sdx = String.Format(" dto2 = {0}", dto2)
    Console.WriteLine(sdx)
    dto3 = New DateTimeOffset(year, month, day, hour, minute, seconds, milliseconds, ts2)
    sdx = String.Format(" dto3 = {0}", dto3)
    Console.WriteLine(sdx)
    dto4 = New DateTimeOffset(year, month, day, hour, minute, seconds, milliseconds, cal, ts3)
    sdx = String.Format(" dto4 = {0} cal = {1}", dto4, cal)
    Console.WriteLine(sdx)
 End Sub
 
Output:

  ' dto1 = 6/15/2020 12:00:00 AM -05:00
  ' dto2 = 6/15/2020 12:00:00 AM +10:00
  ' dto3 = 6/15/2020 12:30:00 AM -10:00
  ' dto4 = 6/28/2020 12:30:00 AM -06:00 cal = System.Globalization.JulianCalendar
   
This is just to familiarize you with the several New constructors in the DateTimeOffset class. The code above uses each available constructor, from the one parameter Date only to the nine parameter constructor using: year, month, day, hour, minute, seconds, milliseconds, TimeSpan, and Calendar. Note that the last constructor using Calendar has a different date than the others, because I used the Julian calendar instead of the Gregorian calendar. The Gregorian calendar is the most common calendar in the western world and most societies recognize its use. The Gregorian calendar was implemented as an improvement over the Julian calendar because the date of Easter tended to slowly drift away from the spring equinox in the Julian calendar, but since Easter is tied to the spring equinox, the Gregorian calendar made adjustments that compensated for this drift. More detail about different calendars will be placed elsewhere under the heading of Globalization in a future installment

Output:

Output:

Output:

Output:

Output:

Output:

Output:

Output:





























































































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

12/08/2015
TheLonelyProgrammer.Net
Alone in a sea of non-programmers
Home DateTime TimeSpans DateTimeOffset