C# Tutorial For Beginners: The DateTime Class
Updated: Jan 19, 2022
The DateTime class is essential as I provide a different way to manage time and dates as part of the execution process of the program. To understand the importance and capabilities of this class, let’s see some examples.
Example 1: Extracting Date/Time information
class Program
{
static void Main(string[] args)
{
//Example 1: Extracting Date/Time information
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Current Time :" + DateTime.Now);
Console.ResetColor();
Console.WriteLine("\nExtract Date: " + DateTime.Now.Date);
Console.WriteLine("Extract current year: " + DateTime.Now.Year);
Console.WriteLine("Extract Day of year: " + DateTime.Now.DayOfYear);
Console.WriteLine("Extract current month " + DateTime.Now.Month);
Console.WriteLine("Extract Day(Number): " + DateTime.Now.Day);
Console.WriteLine("Extract Day of week : " + DateTime.Now.DayOfWeek);
Console.WriteLine("Extract current hour: " + DateTime.Now.Hour);
Console.WriteLine("Extract current minute: " + DateTime.Now.Minute);
Console.WriteLine("Extract current second: " + DateTime.Now.Second);
Console.ReadKey();
}
}
Result:

Example 2: Creating a DateTime instance with a contractor and without
class Program
{
static void Main(string[] args)
{
//Example 1: Default value for a class instance
DateTime DateTimeDefaultValue = new DateTime();
Console.WriteLine("Example 1: Default Value of an instance is: " + DateTimeDefaultValue);
//Example 2: Initialization using the class Constructor(1)
DateTime MyBirthDay = new DateTime(1984,5,31);
Console.WriteLine("Example 2: My Birth Day is: " + MyBirthDay);
//Example 3: Initialization using the class Constructor(2)
DateTime MyBirthDayIncludingTime = new DateTime(1984, 5, 31,22,32,42);
Console.WriteLine("Example 3: My time of birth is: " + MyBirthDayIncludingTime);
//Example 4: Creating instance based on a specific calendar
TaiwanLunisolarCalendar TaiwanLunisolarCalendar_CalanderCal = new TaiwanLunisolarCalendar();
DateTime Ta_Calander = new DateTime(100, 6, 6, TaiwanLunisolarCalendar_CalanderCal);
Console.WriteLine("Example 4: Time Based on Taiwan calendar: " + Ta_Calander);
Console.ReadKey();
}
}
Result:

Example 3: Working with specific date parts
class Program
{
static void Main(string[] args)
{
//Get the current date and time
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Current Time :" + DateTime.Now +"\n");
Console.ResetColor();
//Editing specific parts of the returned values
Console.WriteLine("Current Date + 1 Year: " + DateTime.Now.AddYears(1));
Console.WriteLine("Current Date - 20 Years: " + DateTime.Now.AddYears(-20));
Console.WriteLine("Current Date + 2 Months: " + DateTime.Now.AddMonths(2));
Console.WriteLine("Current Date - 2 Months: " + DateTime.Now.AddMonths(-2));
Console.WriteLine("Current Date + 3 Days: " + DateTime.Now.AddDays(3));
Console.WriteLine("Current Date - 3 Days: " + DateTime.Now.AddDays(-3));
Console.ReadKey();
}
}
Result:

Example 4:
class Program
{
static void Main(string[] args)
{
//Get the current date and time
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Current Time :" + DateTime.Now);
Console.ResetColor();
Console.WriteLine();
//Main methods for time modifications
Console.WriteLine("Current Time + 10 Hours: " + DateTime.Now.AddHours(10));
Console.WriteLine("Current Time - 10 Hours: " + DateTime.Now.AddHours(-10));
Console.WriteLine("Current Time + 5 Minutes: " + DateTime.Now.AddMinutes(5));
Console.WriteLine("Current Time - 5 Minutes: " + DateTime.Now.AddMinutes(-5));
Console.WriteLine("Current Time + 30 Seconds: " + DateTime.Now.AddSeconds(30));
Console.WriteLine("Current Time - 30 Seconds: " + DateTime.Now.AddSeconds(-30));
Console.ReadKey();
}
}
Result:

Example 5: How can we Compare dates?
class Program
{
static void Main(string[] args)
{
DateTime TheFirstDate = new DateTime(2010, 1, 1, 1, 1, 1);
DateTime TheSecondDate = new DateTime(1900, 2, 2, 2, 2, 2);
DateTime TheThirdDate = new DateTime(1900, 2, 2, 2, 2, 2);
//Calling the Comparing method:
CompareDates(TheFirstDate, TheSecondDate);
CompareDates(TheSecondDate, TheThirdDate);
CompareDates(TheThirdDate, TheFirstDate);