---
title: "Read YAML timestamps as DateTimeOffset in C#"
url: https://recursiveai.org/oneyaml/csharp/cookbook/preserve-yaml-timestamp-offset/
description: "Read a YAML timestamp into DateTimeOffset with OneYaml, keep its numeric offset, and print the same instant in UTC without changing the stored value."
version: "1.2.1"
---

# Read YAML timestamps as DateTimeOffset in C#

**OneYaml** is a free, open-source YAML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/OneYaml) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-csharp) · [Docs](https://recursiveai.org/oneyaml/csharp/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/csharp/cookbook/preserve-yaml-timestamp-offset/preserve-yaml-timestamp-offset.zip)

Read a timestamp with its numeric offset intact, then get the same instant in UTC.

```shell
dotnet add package OneYaml
```

Targets .NET 8 and [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (used in the download).

```csharp
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](https://recursiveai.org/oneyaml/csharp/cookbook/preserve-yaml-timestamp-offset/preserve-yaml-timestamp-offset.zip), then `dotnet run`.

Prints the original `+05:30` offset as a duration and the UTC instant as a timestamp.

```text
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`.

```csharp
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:

```text
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`.

```csharp
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; }
}
```

```text
2026-11-19T09:30:00.0000000+05:30
```


## Related recipes

- [Parse YAML into C# classes](https://recursiveai.org/oneyaml/csharp/cookbook/parse-yaml-into-classes/)
- [Write a custom YAML converter](https://recursiveai.org/oneyaml/csharp/cookbook/custom-yaml-converter/)

## 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](https://github.com/recursive-ai-oss/oneyaml-csharp/blob/7818048c4be434333767c47d66176fc626d1da8e/docs/serializer.md#L297-L299).

The [serializer guide](https://github.com/recursive-ai-oss/oneyaml-csharp/blob/7818048c4be434333767c47d66176fc626d1da8e/docs/serializer.md#L50-L51) 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](https://github.com/recursive-ai-oss/oneyaml-csharp/blob/7818048c4be434333767c47d66176fc626d1da8e/src/OneYaml/Serialization/BuiltInConverters.cs#L636-L644) 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](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.equalsexact#remarks) 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.
