Read a timestamp with its numeric offset intact, then get the same instant in UTC.
dotnet add package OneYamlTargets .NET 8 and .NET 10 (used in the download).
using System; using OneYaml; var yaml = "2026-11-19T09:30:00+05:30"; var time = YamlSerializer.Deserialize<DateTimeOffset>( yaml); Console.WriteLine(time.Offset); Console.WriteLine($"{time.UtcDateTime:O}");
Unzip the example, then dotnet run.
Prints the original +05:30 offset as a duration and the UTC instant as a timestamp.
05:30:00 2026-11-19T04:00:00.0000000Z
The full program
The download also serializes the value and reads it back. An invalid timestamp throws YamlException when OneYaml can’t bind it to DateTimeOffset.
using System; using OneYaml; var yaml = "2026-11-19T09:30:00+05:30"; var time = YamlSerializer.Deserialize<DateTimeOffset>( yaml); Console.WriteLine(time.Offset); Console.WriteLine($"{time.UtcDateTime:O}"); var saved = YamlSerializer.Serialize(time); Console.Write(saved); var reloaded = YamlSerializer.Deserialize<DateTimeOffset>(saved); Console.WriteLine(time.EqualsExact(reloaded));
dotnet run prints:
05:30:00 2026-11-19T04:00:00.0000000Z '2026-11-19T09:30:00.0000000+05:30' True
Variations
Read a timestamp from a mapping. Declare the property as DateTimeOffset. By default, the YAML key startsAt binds to the C# property StartsAt.
using System; using OneYaml; var window = YamlSerializer.Deserialize<Window>( "startsAt: 2026-11-19T09:30:00+05:30")!; Console.WriteLine($"{window.StartsAt:O}"); class Window { public DateTimeOffset StartsAt { get; init; } }
2026-11-19T09:30:00.0000000+05:30Related recipes
The short answer
Use YamlSerializer.Deserialize<DateTimeOffset> instead of Deserialize<DateTime> when the numeric offset matters. The input 2026-11-19T09:30:00+05:30 keeps its offset of five hours and thirty minutes ahead of UTC. Calling UtcDateTime gives 04:00 UTC on that day and doesn’t change the stored offset.
How do I deserialize a YAML timestamp without losing its offset?
Choose DateTimeOffset as the target when you read a timestamp scalar, as in the recipe. Use the same type for the corresponding property if the timestamp sits inside a larger mapping. The type is what tells OneYaml to parse the scalar as a date and time with an offset. If you read a value into an untyped object instead, you don’t get a DateTimeOffset in OneYaml’s set of untyped values.
The serializer guide says reading an offset timestamp as DateTime converts it to the machine’s local time. That’s not the same as preserving the offset written in the file. If a timestamp doesn’t supply an offset, the DateTimeOffset converter assumes UTC. Include the offset explicitly in files that need to record it.
Can I serialize a YAML timestamp and get the same value back?
Yes, for the instant and the numeric offset. The full program uses EqualsExact to check both; ordinary DateTimeOffset equality can report two values equal even when they use different offsets for the same UTC instant. OneYaml’s converter writes the round-trip format, which is why the emitted YAML includes seven fractional digits and quotes that weren’t in the input.
A numeric offset doesn’t identify a named time zone. Several places can share the same offset, and an offset alone doesn’t describe daylight-saving changes. If you need the original time zone identifier or the exact timestamp text, keep it separately as a string. Use the typed value for calculations and comparisons.