Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Date, Time and Globalization

The DateTimeOffset struct

Reference for System.DateTimeOffset in C#: a date and time with a UTC offset, how equality works, conversions to UTC and Unix time, and when to choose it.

DateTimeOffset stores a date and time together with the offset from UTC that applies to it, such as +02:00. That pair identifies one exact instant, so there is no ambiguity about what the clock reading meant. It is the right type for timestamps in APIs, logs, databases and audit trails.

It is not a time zone: it remembers the offset, not the rules (daylight saving changes, region names). If you must say "every day at 09:00 Berlin time", store a DateTime or TimeOnly plus a TimeZoneInfo id instead.

C#
var t = new DateTimeOffset(2026, 7, 1, 14, 0, 0, TimeSpan.FromHours(2));
Console.WriteLine(t.UtcDateTime);   // the same instant as a UTC DateTime (12:00)

Try it

C# Same instant, different offsets

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Unix time, parsing and DateTime conversions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public readonly struct DateTimeOffset : IComparable<DateTimeOffset>, IEquatable<DateTimeOffset>, IFormattable, IParsable<DateTimeOffset>
C#
new DateTimeOffset(int year, int month, int day, int hour, int minute, int second, TimeSpan offset)
new DateTimeOffset(DateTime dateTime)                    // offset from Kind
new DateTimeOffset(DateTime dateTime, TimeSpan offset)   // offset must match a Utc/Local Kind
new DateTimeOffset(long ticks, TimeSpan offset)
Member Description
Offset The TimeSpan from UTC. Always a whole number of minutes between -14:00 and +14:00.
DateTime The clock reading in this offset, with Kind set to Unspecified.
UtcDateTime, LocalDateTime The instant as a DateTime of kind Utc or Local.
UtcNow, Now The current instant (UTC offset, or the local offset).
ToOffset(TimeSpan) The same instant with a different offset.
ToUnixTimeSeconds(), ToUnixTimeMilliseconds() Seconds or milliseconds since 1970-01-01 UTC.
FromUnixTimeSeconds(long), FromUnixTimeMilliseconds(long) The reverse conversions.
Equals, EqualsExact, CompareTo Instant comparison, comparison of time and offset, ordering by instant.
Add..., Subtract Arithmetic; subtracting two values gives a TimeSpan between instants.

Errors: an invalid offset throws ArgumentOutOfRangeException (parameter offset). The new DateTimeOffset(DateTime, TimeSpan) constructor throws ArgumentException when the DateTime's kind is Utc and the offset is not zero, or Local and the offset differs from the local offset.

Usage notes

  • Equality is about the instant. ==, Equals, hashing and ordering use UtcDateTime. Two values with different offsets are equal if they are the same moment. Use EqualsExact when the offset also matters.
  • Store UTC, display with an offset. A common pattern is to keep UtcNow in the database and convert with ToOffset or a TimeZoneInfo when showing it.
  • An offset is not a time zone. +01:00 could be Berlin in winter or Lagos all year. Daylight saving rules are only available through TimeZoneInfo; see TimeZoneInfo.
  • Implicit conversion from DateTime reads the Kind: Utc gives offset zero, Local the local offset, and Unspecified is treated as local. In this sandbox local time is UTC, so results look correct and hide the bug that appears on a server in another zone.
  • JSON and ISO 8601. System.Text.Json writes DateTimeOffset as 2026-03-15T09:30:00+05:30, preserving the offset. Parse with DateTimeOffset.Parse or ParseExact.
  • Precision. Same as DateTime: 100 ns ticks, years 1 to 9999.
  • Arithmetic across daylight saving changes is not aware of those changes, because there are no rules in the value: adding 24 hours adds exactly 24 hours.
  • Prefer it over DateTime for event timestamps and for anything that crosses systems or time zones.