Codectionary / Developer documentation / C#

DateTimeOffset and UTC

DateTimeOffset represents a date/time and UTC offset, identifying an instant. It does not store a region’s future daylight-saving rules. Use a .NET 8+ console project; replace Program.cs with this C# example.

Read the syntax

DateTimeOffset represents a date/time and UTC offset, identifying an instant. It does not store a region’s future daylight-saving rules. Use a .NET 8+ console project; replace Program.cs with this C# example.

The displayed hour changes with the offset. Equality compares the instant, so the original and UTC values are equal.

Syntax

new DateTimeOffset(year, month, day, hour, minute, second, offset).ToUniversalTime()

Examples

DateTimeOffset and UTC

Use a .NET 8+ console project; replace Program.cs with this C# example.

using System;
using System.Globalization;
var local = new DateTimeOffset(2024, 7, 1, 9, 0, 0, TimeSpan.FromHours(1));
var utc = local.ToUniversalTime();
Console.WriteLine(utc.ToString("yyyy-MM-dd HH:mm zzz", CultureInfo.InvariantCulture));
Console.WriteLine(local == utc);
  1. Expected output: 2024-07-01 08:00 +00:00 True
  2. The displayed hour changes with the offset. Equality compares the instant, so the original and UTC values are equal.
  3. Use UTC instants for event storage and convert for display.

Best practices

  • Use UTC instants for event storage and convert for display.
  • Use TimeZoneInfo when a named region’s daylight-saving transitions matter.

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading