Try out my new app: MealPlan, a simple weekly meal planning tool for the iPad.

Parsing Dates and Times in .NET

Parsing dates and times using DateTime.Parse and DateTime.ParseExact is simple enough once you know how they work.

It's simple if you know the format of the string you're getting and it exactly matches one of the built-in formats, or if you're willing to let the framework do it's best to parse the string and you're ok with it failing otherwise, but if you need to parse specific formats, you'll need to build your own format specifiers.

There are two ways of parsing date strings: Parse and ParseExact

DateTime.Parse() is the simplest.It tries to figure out the format of the date, and usually does a pretty good job. Here are some examples that work for me, with the result of a DateTime.ToString() on the parsed date:

String to parse

Resulting DateTime.ToString

01-Jan-2005

1/1/2005 12:00:00 AM

01-Jan-2005 12:34 PM

1/1/2005 12:34:00 PM

1/1/05 1:3

1/1/2004 1:03:00 AM

1 march, 2005 1 am

3/1/2005 1:00:00 AM

1995-02-04

2/4/1995 12:00:00 AM

February 3

2/3/2005 12:00:00 AM

10:30

2/15/2005 10:30:00 AM

10 am

2/15/2005 10:00:00 AM

These all did what we expected. DateTime.Parse() is probably the best function to use if the user is typing in a date, and you want it parsed so you can work with it.

If you're receiving your date strings from data (network, etc), and they're in a specific format, check this table to see if there's a predefined format string for your format:

Standard

Format

Example Date

RFC1123

r

Sun, 06 Nov 1994 08:49:37 GMT

ISO 8601 (almost)

s

1993-02-14T13:10:30

Universal Sortable

u

2004-04-30 04:30:00Z

If there is, then you can use ParseExact with the single-character format specifier like this:
// Parse an RFC1123 date<br />
DateTime dt = DateTime.ParseExact(dateString, "r");

There are other strings you can pass to ParseExact, but they're culture-specific, so generally not all that useful.

In many cases, you have a date string in a specific format,which doesn't just work with DateTime.Parse. One example is this one, which is allowed by the the ISO 8601 standard, but not parsed by ParseExact when you specify the ‘s' specifier:

19930214T131030

To parse this, you need to build a string that specifies the format you're expecting, using characters from this table:

Format specifier

Description

d, dd, ddd, dddd

Current day of the month  d, dd=numeric, ddd=abbreviation (ie, ‘Wed'), dddd=spelled out.

f, ff, fff, ffff, fffff, …

Fractions of a second, varying numbers of digits

h, hh

Hours, 12 hour format.

H, HH

Hours, 24 hour format

m, mm

Minutes

M, MM, MMM, MMMM

Month. MM = 2 digit, MMM = abbreviation (ie, 'Jan'), MMMM=Spelled out.

s, ss

Seconds

t, tt

AM/PM indicator.

y, yy, yyyy

Year

z, zz, zzz

Time zone offset from GMT.

:

Time separator.

/

Date separator.

Any other character

Other characters are matched as literals.

So to parse our sample date, you'd use this string:

// Parse an RFC1123 date<br />
DateTime dt = DateTime.ParseExact("19930214T131030", "yyyyMMddTHHmmss");

The help covers this stuff fairly well, once you find the documentation. Look for ‘Standard DateTime Format Strings' and ‘Custom DateTime Format Strings' if you haven't found what you need here (and let me know, so I can add it).

If you're receiving strings in a number of formats and you're not really sure what you're going to get (like, say, if you're writing a program that does something with RSS feed data), it's convenient that you can pass in multiple format specifiers to ParseExact. For example:

string[] formats = new string[] { "r", "s", "u", "yyyyMMddTHHmmss" };<br />
DateTime dt = DateTime.ParseExact(dateString, formats,<br /> CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

This is most likely to get you a date you can use.

Leave a Reply