.NET
DateTime
ISO 8601
programming
coding tutorial

How to create a .NET DateTime from ISO 8601 format

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

ISO 8601 is the timestamp format commonly used in APIs, logs, and JSON payloads, for example 2025-09-23T13:45:30Z. In .NET, you can parse these values into DateTime, but when the string contains a UTC offset, DateTimeOffset is often the safer starting type because it preserves that offset explicitly.

Parse ISO 8601 Into DateTime

For many ISO 8601 strings, DateTime.Parse works directly.

csharp
1using System;
2using System.Globalization;
3
4string input = "2025-09-23T13:45:30Z";
5DateTime value = DateTime.Parse(
6    input,
7    CultureInfo.InvariantCulture,
8    DateTimeStyles.RoundtripKind
9);
10
11Console.WriteLine(value);
12Console.WriteLine(value.Kind);

DateTimeStyles.RoundtripKind helps preserve whether the input was UTC, local, or unspecified.

Prefer DateTimeOffset When Offsets Matter

If the string contains an offset such as +02:00, DateTimeOffset is usually a better model.

csharp
1using System;
2using System.Globalization;
3
4string input = "2025-09-23T13:45:30+02:00";
5DateTimeOffset dto = DateTimeOffset.Parse(
6    input,
7    CultureInfo.InvariantCulture,
8    DateTimeStyles.RoundtripKind
9);
10
11Console.WriteLine(dto);
12Console.WriteLine(dto.Offset);

This keeps the original offset instead of flattening it immediately into a DateTime interpretation.

If you later need a DateTime, convert deliberately:

csharp
DateTime utc = dto.UtcDateTime;
Console.WriteLine(utc);

Use ParseExact for Strict Input

If the format must match exactly, use ParseExact. The round-trip format specifier "O" is commonly used for full ISO 8601 timestamps.

csharp
1using System;
2using System.Globalization;
3
4string input = "2025-09-23T13:45:30.1234567Z";
5DateTime value = DateTime.ParseExact(
6    input,
7    "O",
8    CultureInfo.InvariantCulture,
9    DateTimeStyles.RoundtripKind
10);
11
12Console.WriteLine(value.ToString("O"));

This is a good choice when you want incorrect formats to fail immediately instead of being accepted loosely.

Safer Parsing With TryParse

When the input comes from users or unreliable external systems, prefer TryParse or TryParseExact.

csharp
1using System;
2using System.Globalization;
3
4string input = "2025-09-23T13:45:30Z";
5
6if (DateTime.TryParse(
7    input,
8    CultureInfo.InvariantCulture,
9    DateTimeStyles.RoundtripKind,
10    out var value))
11{
12    Console.WriteLine($"Parsed: {value:o}");
13}
14else
15{
16    Console.WriteLine("Invalid ISO 8601 date");
17}

This avoids using exceptions for normal validation flow.

Date-Only Inputs

An ISO 8601 string is not always a full timestamp. It may be just a date, such as 2025-09-23. In that case, think about whether you really want a DateTime at all. In newer .NET code, DateOnly may be a clearer domain model for pure calendar dates.

If you do parse into DateTime, be explicit about what time and time-zone meaning that date should carry.

Converting After Parsing

Once you have a parsed value, convert it deliberately rather than implicitly. A timestamp with an offset can be rendered in UTC, local time, or preserved with its original offset, and those choices are semantically different even though they refer to the same instant. Making that conversion step explicit keeps later bugs from turning into silent time-zone drift.

That is one reason DateTimeOffset is often a better first parse target even when the rest of the application eventually wants a DateTime.

Common Pitfalls

The biggest pitfall is parsing into DateTime and then ignoring the Kind property. If the application later assumes local time when the value was UTC, conversions and comparisons can become misleading.

Another common mistake is throwing away the original offset too early. If the source string contains +02:00, DateTimeOffset usually preserves intent better than an immediate conversion to a plain DateTime.

Developers also sometimes rely on culture defaults even though ISO 8601 is a standardized machine format. Using invariant culture makes the parsing behavior more predictable.

Summary

  • ISO 8601 strings can usually be parsed directly in .NET.
  • Use DateTime.Parse with DateTimeStyles.RoundtripKind for DateTime parsing.
  • Prefer DateTimeOffset when the input includes a meaningful UTC offset.
  • Use ParseExact with "O" for strict full-precision timestamps.
  • Use TryParse or TryParseExact for untrusted input and keep time-zone semantics explicit.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.