Skip to main content

How to Convert a DateTime to UTC with ToUniversalTime in C#

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

ToUniversalTime() turns a DateTime into UTC, but what it does depends on two things besides the time itself: the time zone of the computer it runs on, and the value's Kind. Here's what it did with a Local, a Utc and an Unspecified time.

Microsoft's guide to time zones explains UTC: Coordinated Universal Time (UTC) is a high-precision, atomic time standard. The world's time zones are expressed as positive or negative offsets from UTC. The API reference describes the method: Converts the value of the current DateTime object to Coordinated Universal Time (UTC). It converts from the local time zone, and the reference defines that one too: The local time zone is the time zone on the computer where the code is executing.

Every DateTime also has a Kind property. The reference: Gets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither. ToUniversalTime reads it first: Starting with the .NET Framework version 2.0, the value returned by the ToUniversalTime method is determined by the Kind property of the current DateTime object.

Syntax​

Syntax
DateTime <utcTime> = <time>.ToUniversalTime();
PartWhat it is
<time>The DateTime to convert. Its Kind is Local, Utc or Unspecified
ToUniversalTime()Converts <time> from the computer's time zone to UTC, depending on its Kind
<utcTime>The new DateTime that the method returns, with its Kind set to Utc

What the method does for each Kind, in the reference's words:

Kind of <time>What ToUniversalTime does
UtcNo conversion is performed.
LocalThe current DateTime object is converted to UTC.
UnspecifiedThe current DateTime object is assumed to be a local time, and the conversion is performed as if Kind were Local.

The Example​

A console app called ToUniversalTime converts a 9:00 meeting time to UTC four times: as a local time in January, as a local time in July, as a time that's already UTC, and as a time created without a Kind. It prints each time with its Kind, then the converted time with its Kind.

Because the answer depends on the computer's time zone, every run on this page was made with the TZ environment variable set to America/New_York, the time zone of New York. The program prints the time zone it found, so you can see that it took effect.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n ToUniversalTime
cd ToUniversalTime
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/ToUniversalTime/ToUniversalTime.csproj:
Determining projects to restore...
Restored /Users/you/source/ToUniversalTime/ToUniversalTime.csproj (in 46 ms).
Restore succeeded.

dotnet new console -n ToUniversalTime creates a folder named ToUniversalTime with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or ToUniversalTime.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the computer's time zone:

Program.cs (the time zone)
// ToUniversalTime converts from the time zone of the computer it runs on.
Console.WriteLine($"This computer's time zone: {TimeZoneInfo.Local.Id}");

TimeZoneInfo.Local is the time zone of the computer, and Id is its identifier, such as America/New_York.

Then two local times:

Program.cs (local times)
// A meeting at 9:00 on this computer's clock, in January and in July.
DateTime january = new DateTime(2026, 1, 15, 9, 0, 0, DateTimeKind.Local);
DateTime july = new DateTime(2026, 7, 15, 9, 0, 0, DateTimeKind.Local);
Show("Local, January", january);
Show("Local, July", july);

The last argument, DateTimeKind.Local, sets the Kind: these are 9:00 on this computer's clock. Show is a small function at the end of the file that converts and prints.

Then a time that's already UTC:

Program.cs (a UTC time)
// A time that's already UTC isn't converted.
DateTime utc = new DateTime(2026, 1, 15, 9, 0, 0, DateTimeKind.Utc);
Show("Utc", utc);

DateTimeKind.Utc marks this 9:00 as a UTC time.

Then a time with no Kind:

Program.cs (no kind)
// Without a kind, the value is Unspecified, and ToUniversalTime treats it as local time.
DateTime unspecified = new DateTime(2026, 1, 15, 9, 0, 0);
Show("Unspecified", unspecified);

This constructor has no DateTimeKind argument. The reference: The Kind property is initialized to Unspecified.

Last, the function that does the conversion:

Program.cs (Show)
// Prints a time and its Kind, then the same time converted to UTC and its Kind.
static void Show(string label, DateTime time)
{
DateTime inUtc = time.ToUniversalTime();
Console.WriteLine($"{label,-15} {time:yyyy-MM-dd HH:mm} {time.Kind,-11} -> {inUtc:yyyy-MM-dd HH:mm} {inUtc.Kind}");
}

time.ToUniversalTime() returns a new DateTime, and inUtc keeps it. The line prints the time and its Kind, an arrow, then the converted time and its Kind. The ,-15 in {label,-15} and the ,-11 after time.Kind pad the text with spaces to 15 and 11 characters, so the columns line up.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
This computer's time zone: America/New_York
Local, January 2026-01-15 09:00 Local -> 2026-01-15 14:00 Utc
Local, July 2026-07-15 09:00 Local -> 2026-07-15 13:00 Utc
Utc 2026-01-15 09:00 Utc -> 2026-01-15 09:00 Utc
Unspecified 2026-01-15 09:00 Unspecified -> 2026-01-15 14:00 Utc

macOS-style terminal window titled Terminal — ToUniversalTime showing dotnet run on a computer in the America/New_York time zone, where 9:00 Local becomes 14:00 Utc in January and 13:00 Utc in July, 9:00 Utc stays 9:00 Utc, and 9:00 Unspecified becomes 14:00 Utc

The command was TZ=America/New_York dotnet run, so the first line reports New York's time zone. On your computer it reports yours, and the Local and Unspecified lines convert from your time zone instead.

In January, 9:00 Local became 14:00 Utc. In July, the same 9:00 became 13:00, not 14:00. The reference explains why: The conversion also takes into account the daylight saving time rule that applies to the time represented by the current DateTime object. The Utc time stayed at 9:00, because there was nothing to convert. The Unspecified time became 14:00 Utc, exactly like the local one: without a Kind, ToUniversalTime assumed a local time. Every converted value came back with its Kind set to Utc.

The Complete File​

macOS-style code window titled Program.cs — ToUniversalTime showing the computer's time zone printed, 9:00 meeting times created as Local in January and July, as Utc and without a kind, and a Show function that calls ToUniversalTime and prints both times with their Kind

Program.cs
// ToUniversalTime converts from the time zone of the computer it runs on.
Console.WriteLine($"This computer's time zone: {TimeZoneInfo.Local.Id}");

// A meeting at 9:00 on this computer's clock, in January and in July.
DateTime january = new DateTime(2026, 1, 15, 9, 0, 0, DateTimeKind.Local);
DateTime july = new DateTime(2026, 7, 15, 9, 0, 0, DateTimeKind.Local);
Show("Local, January", january);
Show("Local, July", july);

// A time that's already UTC isn't converted.
DateTime utc = new DateTime(2026, 1, 15, 9, 0, 0, DateTimeKind.Utc);
Show("Utc", utc);

// Without a kind, the value is Unspecified, and ToUniversalTime treats it as local time.
DateTime unspecified = new DateTime(2026, 1, 15, 9, 0, 0);
Show("Unspecified", unspecified);

// Prints a time and its Kind, then the same time converted to UTC and its Kind.
static void Show(string label, DateTime time)
{
DateTime inUtc = time.ToUniversalTime();
Console.WriteLine($"{label,-15} {time:yyyy-MM-dd HH:mm} {time.Kind,-11} -> {inUtc:yyyy-MM-dd HH:mm} {inUtc.Kind}");
}

Common Mistakes​

MistakeWhat happensDo this instead
Converting a UTC time that was read back from textDateTime.Parse("2026-01-15 09:00", CultureInfo.InvariantCulture) had the Kind Unspecified, so ToUniversalTime() moved it from 09:00 to 14:00Mark it as UTC first: DateTime.SpecifyKind(saved, DateTimeKind.Utc) kept 09:00 with the Kind Utc, and ToUniversalTime() then left it at 09:00
Using ToUniversalTime for a time in another time zone9:00 in Tokyo became 14:00, converted from New York, the computer's time zoneName the time zone: TimeZoneInfo.ConvertTimeToUtc(tokyoTime, TimeZoneInfo.FindSystemTimeZoneById("Asia/Tokyo")) gave 00:00
Comparing a Local and a Utc value with ==09:00 Local == 14:00 Utc, the same moment, was False, while 09:00 Local == 09:00 Utc, five hours apart, was TrueConvert both with ToUniversalTime() before comparing: then the same moment gave True
Calling meeting.ToUniversalTime(); on its own linemeeting still printed 09:00 LocalKeep the result: after meeting = meeting.ToUniversalTime();, it printed 14:00 Utc

FAQ​

Why does == ignore the Kind?​

Because it compares only the stored date and time. The reference for the == operator: The Equality operator determines whether two DateTime values are equal by comparing their number of ticks. Before comparing DateTime objects, make sure that the objects represent times in the same time zone. You can do this by comparing the values of their Kind property.

What does SpecifyKind change?​

Only the Kind, never the time. The reference: The returned DateTime value does not represent the same instant in time as the value parameter, and SpecifyKind is not a time zone conversion method. Instead, it leaves the time specified by the value parameter unchanged, and sets the Kind property to kind. That's why it's the fix for a UTC time whose Kind was lost, and the wrong tool for converting.

How do I go back from UTC to local time?​

With ToLocalTime(). The reference: Converts the value of the current DateTime object to local time. On the New York computer, 14:00 Utc became 09:00 Local.

Conclusion​

ToUniversalTime() converts a DateTime from the time zone of the computer it runs on to UTC, and the Kind decides what happens: Local and Unspecified times are converted, and Utc times are left as they are. Set the right Kind when you create or read a time, use TimeZoneInfo.ConvertTimeToUtc for any other time zone, and keep the value the method returns.

Additional Resources​