Get the System Date and Time

Problem

You need to retrieve the current date and time.

Solution

Use the DateTime.Now shared property.

Discussion

DateTime.Now retrieves the current date and time as a DateTime structure. You can then retrieve specific date information from its properties, such as Month, Day, Date, Hour, Millisecond, and even DayOfWeek.

Dim Now As DateTime = DateTime.Now

Console.WriteLine("The current date is: " & Now.ToString())
Console.WriteLine("It's a " & Now.DayOfWeek.ToString())

The output for this code is as follows:

The current date is: 2002-10-31 5:24:14 PM
It's a Thursday

If you want to retrieve only the date portion of the current day, you can use the DateTime.Today shared method. The time portion will be set to 12:00 AM (00:00:00). ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.