C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In C#, the DateTime
struct is used to represent dates and times. It is part of the System
namespace. In this tutorial, we'll cover the basics of the DateTime
class, including creating, formatting, and manipulating DateTime
objects.
To create a DateTime
object, you can use the constructor or the static properties and methods provided by the class.
Current date and time:
DateTime now = DateTime.Now;
Current date (time set to midnight):
DateTime today = DateTime.Today;
Specific date and time:
DateTime customDate = new DateTime(2023, 4, 27, 14, 30, 0);
You can format a DateTime
object as a string using the ToString()
method and format specifiers.
Default format:
string defaultFormat = now.ToString(); // e.g., "04/27/2023 14:30:00"
Custom format:
string customFormat = now.ToString("yyyy-MM-dd HH:mm:ss"); // e.g., "2023-04-27 14:30:00"
The DateTime
class provides methods and properties to manipulate dates and times.
Add or subtract time:
DateTime oneHourLater = now.AddHours(1); DateTime oneDayEarlier = now.AddDays(-1);
Access date and time components:
int year = now.Year; int month = now.Month; int day = now.Day; int hour = now.Hour; int minute = now.Minute; int second = now.Second; DayOfWeek dayOfWeek = now.DayOfWeek;
You can compare DateTime
objects using the standard comparison operators, such as <
, >
, ==
, and !=
.
DateTime date1 = new DateTime(2023, 4, 27); DateTime date2 = new DateTime(2023, 4, 28); bool areEqual = date1 == date2; // false bool isBefore = date1 < date2; // true
You can create a DateTime
object from a string using the Parse()
and TryParse()
methods.
Parse (throws an exception if the input is not a valid date):
DateTime parsedDate = DateTime.Parse("2023-04-27");
TryParse (returns a boolean indicating whether the parsing was successful):
DateTime parsedDate; bool success = DateTime.TryParse("2023-04-27", out parsedDate);
Working with DateTime in C#:
DateTime
class is commonly used for representing and manipulating date and time values.DateTime currentDateTime = DateTime.Now;
Creating DateTime objects in C#:
DateTime
objects with specific date and time values.DateTime specificDateTime = new DateTime(2023, 12, 31, 15, 30, 0);
DateTime formatting in C#:
DateTime
values as strings using custom format strings.string formattedDate = currentDateTime.ToString("yyyy-MM-dd HH:mm:ss");
Parsing DateTime strings in C#:
DateTime
objects.string dateString = "2023-12-31"; DateTime parsedDate = DateTime.Parse(dateString);
Adding and subtracting time with DateTime in C#:
DateTime
objects.DateTime futureDate = currentDateTime.AddMonths(3); DateTime pastDate = currentDateTime.AddDays(-7);
DateTime comparison in C#:
DateTime
values to determine their order.DateTime earlierDate = new DateTime(2023, 1, 1); bool isLater = currentDateTime > earlierDate;
DateTime manipulation in C#:
DateTime
values using methods like AddDays
, AddMonths
, and more.DateTime newDate = currentDateTime.AddDays(5).AddMonths(2);
Handling time zones with DateTime in C#:
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); DateTime utcDateTime = DateTime.UtcNow; DateTime convertedDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, timeZone);