C# DateTime Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To convert a UTC DateTime
to an ISO 8601 date format string in C#, you can use the ToString
method with the format specifier "yyyy-MM-ddTHH:mm:ss.fffZ"
, where Z
indicates that the string represents a UTC time. Here's an example:
DateTime utcDateTime = DateTime.UtcNow; string iso8601String = utcDateTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
To convert a UTC DateTime
to the equivalent local DateTime
in C#, you can use the ToLocalTime
method of the DateTime
object. Here's an example:
DateTime utcDateTime = DateTime.UtcNow; DateTime localDateTime = utcDateTime.ToLocalTime();
Note that the ToLocalTime
method converts the DateTime
object to the local time zone of the computer running the code. If you need to convert the DateTime
object to a specific time zone, you can use the TimeZoneInfo
class. Here's an example:
DateTime utcDateTime = DateTime.UtcNow; TimeZoneInfo timezone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, timezone);
In this example, we are converting the UTC DateTime
object to the Pacific Standard Time zone. You can replace "Pacific Standard Time"
with the ID of the time zone you want to convert to.
C# convert local time to UTC DateTime:
Converting a local DateTime to UTC involves using the ToUniversalTime
method.
DateTime localDateTime = DateTime.Now; DateTime utcDateTime = localDateTime.ToUniversalTime(); Console.WriteLine($"Converted UTC DateTime: {utcDateTime}");
Working with UTC DateTime in C#: Working with UTC DateTime involves ensuring that DateTime objects are in the UTC format.
DateTime utcDateTime = DateTime.UtcNow; Console.WriteLine($"Current UTC DateTime: {utcDateTime}");
Set DateTime to UTC in C#:
Setting a DateTime to UTC involves using the SpecifyKind
method.
DateTime localDateTime = DateTime.Now; DateTime utcDateTime = DateTime.SpecifyKind(localDateTime, DateTimeKind.Utc); Console.WriteLine($"UTC DateTime: {utcDateTime}");
Convert UTC DateTime to local time in C#:
Converting a UTC DateTime to local time is done using the ToLocalTime
method.
DateTime utcDateTime = DateTime.UtcNow; DateTime localDateTime = utcDateTime.ToLocalTime(); Console.WriteLine($"Converted Local DateTime: {localDateTime}");
Parsing UTC DateTime from string in C#:
Parsing a UTC DateTime from a string using DateTimeStyles
.
string utcDateTimeString = "2023-01-01T12:00:00Z"; DateTime parsedUtcDateTime = DateTime.ParseExact(utcDateTimeString, "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); Console.WriteLine($"Parsed UTC DateTime: {parsedUtcDateTime}");
Difference between UTC and local time in C#: Calculating the time difference between UTC and local time.
DateTime utcDateTime = DateTime.UtcNow; DateTime localDateTime = DateTime.Now; TimeSpan timeDifference = localDateTime - utcDateTime; Console.WriteLine($"Time difference: {timeDifference}");
C# DateTime.UtcNow usage:
DateTime.UtcNow
is a convenient way to get the current UTC time.
DateTime currentUtcTime = DateTime.UtcNow; Console.WriteLine($"Current UTC Time: {currentUtcTime}");
DateTimeKind.Utc in C# DateTime: Specifying the DateTimeKind of a DateTime object as Utc.
DateTime utcDateTime = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); Console.WriteLine($"UTC DateTime: {utcDateTime}");
Adjusting DateTime for different time zones in C#:
Using TimeZoneInfo
to convert DateTime between different time zones.
DateTime localDateTime = DateTime.Now; TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); DateTime pacificDateTime = TimeZoneInfo.ConvertTime(localDateTime, timeZoneInfo); Console.WriteLine($"Converted DateTime in Pacific Time: {pacificDateTime}");
Handling daylight saving time with UTC DateTime in C#: Using UTC DateTime helps avoid issues with daylight saving time.
DateTime utcDateTime = DateTime.UtcNow; Console.WriteLine($"UTC DateTime with daylight saving time handling: {utcDateTime}");