Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Working with DateTime in C#
The DateTime class in C# is used to represent dates and times. It provides properties and methods to work with date and time values, perform calculations, comparisons, and formatting operations.
Syntax
Following is the syntax for creating DateTime objects −
DateTime variableName = new DateTime(year, month, day); DateTime variableName = new DateTime(year, month, day, hour, minute, second); DateTime variableName = DateTime.Now; // current date and time DateTime variableName = DateTime.Today; // current date only
Common DateTime Properties
| Property | Description |
|---|---|
| Date | Gets the date component (time set to 00:00:00) |
| Day | Gets the day of the month (1-31) |
| Month | Gets the month component (1-12) |
| Year | Gets the year component |
| Hour | Gets the hour component (0-23) |
| Minute | Gets the minute component (0-59) |
| Second | Gets the second component (0-59) |
| DayOfWeek | Gets the day of the week (Sunday, Monday, etc.) |
Creating DateTime Objects
Example
using System;
class Program {
static void Main() {
// Different ways to create DateTime objects
DateTime specificDate = new DateTime(2024, 3, 15);
DateTime specificDateTime = new DateTime(2024, 3, 15, 14, 30, 25);
DateTime currentDateTime = DateTime.Now;
DateTime currentDate = DateTime.Today;
Console.WriteLine("Specific date: " + specificDate);
Console.WriteLine("Specific date and time: " + specificDateTime);
Console.WriteLine("Current date and time: " + currentDateTime);
Console.WriteLine("Current date only: " + currentDate);
}
}
The output of the above code is −
Specific date: 3/15/2024 12:00:00 AM Specific date and time: 3/15/2024 2:30:25 PM Current date and time: 3/15/2024 10:45:12 AM Current date only: 3/15/2024 12:00:00 AM
Accessing DateTime Properties
Example
using System;
class Program {
static void Main() {
DateTime dt = new DateTime(2024, 3, 15, 14, 30, 25);
Console.WriteLine("Full DateTime: " + dt);
Console.WriteLine("Date only: " + dt.Date);
Console.WriteLine("Year: " + dt.Year);
Console.WriteLine("Month: " + dt.Month);
Console.WriteLine("Day: " + dt.Day);
Console.WriteLine("Hour: " + dt.Hour);
Console.WriteLine("Minute: " + dt.Minute);
Console.WriteLine("Second: " + dt.Second);
Console.WriteLine("Day of week: " + dt.DayOfWeek);
}
}
The output of the above code is −
Full DateTime: 3/15/2024 2:30:25 PM Date only: 3/15/2024 12:00:00 AM Year: 2024 Month: 3 Day: 15 Hour: 14 Minute: 30 Second: 25 Day of week: Friday
Comparing DateTime Objects
Example
using System;
class Program {
static void Main() {
DateTime date1 = new DateTime(2024, 7, 20);
DateTime date2 = new DateTime(2024, 7, 25);
Console.WriteLine("Date 1: " + date1);
Console.WriteLine("Date 2: " + date2);
if (date1 < date2) {
Console.WriteLine("{0} comes before {1}", date1.ToShortDateString(), date2.ToShortDateString());
}
if (date1.Equals(date2)) {
Console.WriteLine("Both dates are equal");
} else {
Console.WriteLine("Dates are different");
}
int comparison = DateTime.Compare(date1, date2);
if (comparison < 0) {
Console.WriteLine("Date1 is earlier than Date2");
} else if (comparison > 0) {
Console.WriteLine("Date1 is later than Date2");
} else {
Console.WriteLine("Both dates are the same");
}
}
}
The output of the above code is −
Date 1: 7/20/2024 12:00:00 AM Date 2: 7/25/2024 12:00:00 AM 7/20/2024 comes before 7/25/2024 Dates are different Date1 is earlier than Date2
DateTime Formatting
Example
using System;
class Program {
static void Main() {
DateTime dt = new DateTime(2024, 3, 15, 14, 30, 25);
Console.WriteLine("Default format: " + dt);
Console.WriteLine("Short date: " + dt.ToShortDateString());
Console.WriteLine("Long date: " + dt.ToLongDateString());
Console.WriteLine("Short time: " + dt.ToShortTimeString());
Console.WriteLine("Long time: " + dt.ToLongTimeString());
Console.WriteLine("Custom format: " + dt.ToString("yyyy-MM-dd HH:mm:ss"));
Console.WriteLine("Month/Year: " + dt.ToString("MMMM yyyy"));
}
}
The output of the above code is −
Default format: 3/15/2024 2:30:25 PM Short date: 3/15/2024 Long date: Friday, March 15, 2024 Short time: 2:30 PM Long time: 2:30:25 PM Custom format: 2024-03-15 14:30:25 Month/Year: March 2024
Conclusion
The DateTime class in C# provides comprehensive functionality for working with dates and times. It offers properties to access individual components, methods for comparison and formatting, and supports various ways to create and manipulate date-time values for different programming scenarios.
Advertisements
