- 04 May 2024
- 2 Minutes to read
- DarkLight
Date-Time
- Updated on 04 May 2024
- 2 Minutes to read
- DarkLight
General
Values of type DateTime (alternativly named Date) can hold dates or date-times. A detailed explanation of the data-type can be found in the Date Data-Type article of the Visual Basic .NET language documentation.
Constants
Constant date-time values are written using the following syntax:
#8/13/2002 12:14 PM#
You can also only specify a date:
#8/13/2002#
To access the current date-time, you can use the following expression:
DateTime.Now
To access the current date, you can use the following expression:
Date.Today
Calculations
AddDays
The AddDays function returns a date that has the provided number of days added or subtracted; the following expression returns the date for yesterday:
Date.Today.AddDays(-1)
AddMonths
The AddMonths function returns a date that has the provided number of months added or subtracted; the following expression returns today minus 1 month:
Date.Today.AddMonths(-1)
Formatting and Conversions
You can convert a date-time into a text representation by using the ToString function. This conversion
process uses the culture settings of the user under which the expression is being executed or another
culture provided by the context, e.g. culture-aware reports. If you use the
following syntax, the date-time value will be converted to a textual representation using the default
format of the applicable culture:
<Date_Value>.ToString()
For instance, if you want to convert the current, local date-time into a textual representation using
the default format of the applicable culture, you can use the following syntax:
Date.Now.ToString()
However, the ToString function also allows you to supply a standard format string (🌐 Standard Date
Time Format Strings Documentation) or a more flexible custom format string (🌐 Custom Date Time
Format Strings Documentation). For instance, if you want to convert the current, local date-time into
a textual representation that contains the 2-digit day, then a dot, then the 2-digit month, then a dot,
and then the 4-digit year by using a custom format string, you can use the following syntax:
Date.Now.ToString("dd.MM.yyyy")
If you want to include time-information in the textual representation (2-digit hour in 24-hour format,
then ":", then 2-digit minute), then you can use the following syntax:
Date.Now.ToString("dd.MM.yyyy HH:mm")
If you want to convert the current, local date into a textual representation according to the short date
pattern of the current culture, you can use the following syntax:
Date.Now.ToString("d")