KavenG
KavenG

Reputation: 171

How to parse an HTTP Last-Modified header from UTC to DateTime in C#

I'm trying to parse the date from the Last-Modified header in an HTTP response.

The date shows as follow:

Last-Modified: Sat, 01 Jul 2006 01:50:55 UTC

I tried DateTime.Parse, DateTime.ParseExact with no success.

What is that UTC thing at the end and why does C# doesn't want to parse it?

Update:

Upvotes: 9

Views: 6276

Answers (1)

JLe
JLe

Reputation: 2904

Use ParseExact to specify the input format:

string inputDate = "Sat, 01 Jul 2006 01:50:55 UTC";

DateTime time = DateTime.ParseExact(inputDate,
                    "ddd, dd MMM yyyy HH:mm:ss 'UTC'",
                    CultureInfo.InvariantCulture.DateTimeFormat,
                    DateTimeStyles.AssumeUniversal);

Upvotes: 11

Related Questions