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
Convert the value of the current DateTime object to UTC in C#
To convert the value of the current DateTime object to Coordinated Universal Time (UTC), C# provides the ToUniversalTime() method. This method converts a local time or unspecified DateTime to its equivalent UTC representation.
Syntax
Following is the syntax for the ToUniversalTime() method −
DateTime utcDateTime = localDateTime.ToUniversalTime();
Return Value
The ToUniversalTime() method returns a new DateTime object that represents the UTC equivalent of the original DateTime. If the original DateTime's Kind property is DateTimeKind.Utc, it returns the same value unchanged.
Converting Local DateTime to UTC
Example
using System;
public class Demo {
public static void Main() {
DateTime localDate = new DateTime(2019, 12, 11, 7, 11, 25, DateTimeKind.Local);
Console.WriteLine("Local Date = {0}", localDate);
Console.WriteLine("Local Kind = {0}", localDate.Kind);
DateTime utcDate = localDate.ToUniversalTime();
Console.WriteLine("UTC Date = {0}", utcDate);
Console.WriteLine("UTC Kind = {0}", utcDate.Kind);
}
}
The output of the above code is −
Local Date = 12/11/2019 7:11:25 AM Local Kind = Local UTC Date = 12/11/2019 1:11:25 PM UTC Kind = Utc
Using ToUniversalTime() with Current System Time
Example
using System;
public class Demo {
public static void Main() {
DateTime localNow = DateTime.Now;
DateTime utcNow = DateTime.UtcNow;
DateTime convertedUtc = localNow.ToUniversalTime();
Console.WriteLine("Local time: {0}", localNow);
Console.WriteLine("Direct UTC: {0}", utcNow);
Console.WriteLine("Converted UTC: {0}", convertedUtc);
Console.WriteLine("Times match: {0}", utcNow.ToString("yyyy-MM-dd HH:mm:ss") == convertedUtc.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
The output of the above code is −
Local time: 12/15/2023 3:30:45 PM Direct UTC: 12/15/2023 9:30:45 PM Converted UTC: 12/15/2023 9:30:45 PM Times match: True
Handling Different DateTimeKind Values
| DateTimeKind | ToUniversalTime() Behavior |
|---|---|
| Local | Converts from local time zone to UTC by adding the time zone offset. |
| Utc | Returns the same DateTime unchanged since it's already UTC. |
| Unspecified | Assumes the DateTime is local time and converts it to UTC. |
Example
using System;
public class Demo {
public static void Main() {
DateTime baseTime = new DateTime(2023, 6, 15, 14, 30, 0);
DateTime localTime = DateTime.SpecifyKind(baseTime, DateTimeKind.Local);
DateTime utcTime = DateTime.SpecifyKind(baseTime, DateTimeKind.Utc);
DateTime unspecifiedTime = DateTime.SpecifyKind(baseTime, DateTimeKind.Unspecified);
Console.WriteLine("Local ({0}) to UTC: {1}", localTime.Kind, localTime.ToUniversalTime());
Console.WriteLine("UTC ({0}) to UTC: {1}", utcTime.Kind, utcTime.ToUniversalTime());
Console.WriteLine("Unspecified ({0}) to UTC: {1}", unspecifiedTime.Kind, unspecifiedTime.ToUniversalTime());
}
}
The output of the above code is −
Local (Local) to UTC: 6/15/2023 8:30:00 PM UTC (Utc) to UTC: 6/15/2023 2:30:00 PM Unspecified (Unspecified) to UTC: 6/15/2023 8:30:00 PM
Conclusion
The ToUniversalTime() method in C# converts DateTime objects to their UTC equivalent by applying the appropriate time zone offset. It's essential for applications that need to work with times across different time zones or store timestamps in a standardized format.
