Class
DateInterval
Description
A DateInterval is used to modify a date object. You specify how much you want to offset the date by and then add (or subtract) the interval to the date to get a new date object.
The resulting DateInterval always uses the largest grouping first. For example, if the delta is 367 days, the interval's Years property will be 1 and the Days property will be 2.
Properties
Name |
Type |
Read-Only |
Shared |
---|---|---|---|
Methods
Name |
Parameters |
Returns |
Shared |
---|---|---|---|
years As Integer = 0, months As Integer = 0, days As Integer = 0, hours As Integer = 0, minutes As Integer = 0, seconds As Integer = 0, nanoseconds As Integer = 0 |
Property descriptions
DateInterval.Days
Days As Integer
The number of days in the interval.
Var d1 As New DateTime(2024, 5, 1)
Var d2 As New DateTime(2024, 5, 20)
Var interval As DateInterval
interval = d2 - d1
' interval.Days = 19
The result is not always measured in just days. For example:
Var d1 As New DateTime(2024, 5, 1)
Var d2 As New DateTime(2024, 6, 1)
Var interval As DateInterval
interval = d2 - d1
In this example, the interval.Days is 0 and the interval.Months is 1. To continue:
Var d1 As New DateTime(2024, 5, 1)
Var d2 As New DateTime(2024, 6, 5)
Var interval As DateInterval
interval = d2 - d1
In this example, interval.Months is 1 and interval.Days is 4.
DateInterval.Hours
Hours As Integer
The number of hours in the interval.
DateInterval.Minutes
Minutes As Integer
The number of minutes in the interval.
DateInterval.Months
Months As Integer
The number of months in the interval.
Get a date two months from today:
Var di As New DateInterval
di.Months = 2 ' 2 months
' Get date two months from today
Var d2 As DateTime = DateTime.Now + di
DateInterval.Nanoseconds
Nanoseconds As Integer
The number of nanoseconds in the interval.
DateInterval.Seconds
Seconds As Integer
The number of seconds in the interval.
DateInterval.Years
Years As Integer
The number of years in the interval.
Var d1 As New DateTime(2017, 5, 1)
Var d2 As New DateTime(2030, 1, 1)
Var interval As DateInterval
interval = d2 - d1
' interval.Years = 12
Method descriptions
DateInterval.Constructor
Constructor(years As Integer = 0, months As Integer = 0, days As Integer = 0, hours As Integer = 0, minutes As Integer = 0, seconds As Integer = 0, nanoseconds As Integer = 0)
Note
Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.
Specifies an interval using the various properties to create a date and time.
Creates a date that is two months from today and two months before today:
Var di As New DateInterval(0, 2) ' 0 years, 2 months
' Get date two months from today
Var d1 As DateTime = DateTime.Now + di
' Get date two months before today
Var d2 As DateTime = DateTime.Now - di
Sample code
This example adds 5 days to the current date:
Var d As DateTime = DateTime.Now
Var interval As New DateInterval(0, 0, 5)
d = d + interval
MessageBox(d.ToString)
This example adds 1 year, 3 months and 4 days to the current date:
Var d As DateTime = DateTime.Now
Var interval As New DateInterval(1, 3, 4)
d = d + interval
MessageBox(d.ToString)
This example adds 8 hours to the current date:
Var d As DateTime = DateTime.Now
Var interval As New DateInterval(0, 0, 0, 8)
d = d + interval
MessageBox(d.ToString)
Compatibility
All project types on all supported operating systems.