C# DateTime Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
You can use the DateTime.Parse
or DateTime.ParseExact
methods to convert a string to a DateTime
object in C#. Here's an example:
string dateString = "2022-03-28"; DateTime dateTime = DateTime.Parse(dateString); // or DateTime.ParseExact(dateString, "yyyy-MM-dd", CultureInfo.InvariantCulture);
In the above example, we have a string
that represents a date in the format "yyyy-MM-dd". We then use the DateTime.Parse
or DateTime.ParseExact
method to convert the string to a DateTime
object.
You can use the DateTime.Now
property to get the current date and time, and append it to a file name to create a timestamp. Here's an example:
string fileName = "example.txt"; string timeStamp = DateTime.Now.ToString("yyyyMMddHHmmss"); string newFileName = $"{fileName}_{timeStamp}";
In the above example, we have a string
that represents a file name. We then use the DateTime.Now
property to get the current date and time, and format it as a string in the format "yyyyMMddHHmmss". We then append the timestamp to the file name using string interpolation to create a new file name.
You can use the DateTime.Ticks
property to convert a DateTime
object to a long
value in C#. Here's an example:
DateTime dateTime = DateTime.Now; long ticks = dateTime.Ticks;
In the above example, we have a DateTime
object that represents the current date and time. We then use the DateTime.Ticks
property to get the number of ticks that have elapsed since January 1, 0001 at 12:00:00 midnight, and assign it to a long
variable.
You can use the DateTime.TimeOfDay
property to get the time part of a DateTime
object in C#. Here's an example:
DateTime dateTime = DateTime.Now; TimeSpan time = dateTime.TimeOfDay;
In the above example, we have a DateTime
object that represents the current date and time. We then use the DateTime.TimeOfDay
property to get the time part of the DateTime
object as a TimeSpan
.
You can use the DateTime.Millisecond
property to get the milliseconds component of a DateTime
object in C#. Here's an example:
DateTime dateTime = DateTime.Now; int milliseconds = dateTime.Millisecond;
In the above example, we have a DateTime
object that represents the current date and time. We then use the DateTime.Millisecond
property to get the milliseconds component of the DateTime
object as an int
.
Parsing DateTime in C#
Parsing DateTime in C# involves converting a string representation of a date and time into a DateTime object.
string dateString = "2023-12-21"; DateTime parsedDate = DateTime.Parse(dateString);
Formatting DateTime in C#
Formatting DateTime in C# allows you to represent a DateTime object as a string in a specific format.
DateTime currentDate = DateTime.Now; string formattedDate = currentDate.ToString("yyyy-MM-dd HH:mm:ss");
Current DateTime in C#
Obtain the current DateTime in C# using the DateTime.Now property.
DateTime currentDateTime = DateTime.Now;
Adding and subtracting time with DateTime in C#
Perform addition and subtraction on DateTime objects to manipulate dates and times.
DateTime currentDate = DateTime.Now; DateTime futureDate = currentDate.AddHours(24); DateTime pastDate = currentDate.AddMonths(-1);
Comparing DateTime objects in C#
Compare DateTime objects in C# to determine their relative order.
DateTime date1 = DateTime.Parse("2023-01-01"); DateTime date2 = DateTime.Parse("2023-02-01"); if (date1 < date2) { // date1 is earlier than date2 }
DateTime operations and calculations in C#
Perform various DateTime operations and calculations, such as finding the difference between two dates.
DateTime start = DateTime.Parse("2023-01-01"); DateTime end = DateTime.Parse("2023-12-31"); TimeSpan duration = end - start;
Converting DateTime to string in C#
Convert a DateTime object to a string using the ToString method.
DateTime currentDate = DateTime.Now; string dateString = currentDate.ToString();
Parsing custom date formats in C#
Parse DateTime with custom date formats using DateTime.ParseExact.
string dateString = "12/21/2023"; DateTime parsedDate = DateTime.ParseExact(dateString, "MM/dd/yyyy", null);
Working with time zones in C# DateTime
Adjust DateTime to a specific time zone using the TimeZoneInfo class.
DateTime utcTime = DateTime.UtcNow; TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, timeZone);
DateTime.TryParse vs. DateTime.Parse in C#
Use DateTime.TryParse to avoid exceptions when parsing DateTime from a string.
string dateString = "2023-12-21"; DateTime parsedDate; if (DateTime.TryParse(dateString, out parsedDate)) { // Parsing successful }
DateTime manipulation using TimeSpan in C#
Use TimeSpan for DateTime manipulation, such as adding or subtracting intervals.
DateTime startDate = DateTime.Parse("2023-01-01"); TimeSpan interval = TimeSpan.FromDays(30); DateTime endDate = startDate + interval;
Handling null DateTime in C#
Handle null DateTime by using nullable DateTime (DateTime?).
DateTime? nullableDate = null;
DateTime offset in C#
Use DateTimeOffset to represent a point in time with an offset from UTC.
DateTimeOffset dateTimeOffset = DateTimeOffset.Now;