Saturday, 17 May 2014

Parsing Dates and Times in .NET

Post By: Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

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 parseResulting DateTime.ToString
01-Jan-20051/1/2005 12:00:00 AM
01-Jan-2005 12:34 PM1/1/2005 12:34:00 PM
1/1/05 1:31/1/2004 1:03:00 AM
1 march, 2005 1 am3/1/2005 1:00:00 AM
1995-02-042/4/1995 12:00:00 AM
February 32/3/2005 12:00:00 AM
10:302/15/2005 10:30:00 AM
10 am2/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:

StandardFormatExample Date
RFC1123r
Sun, 06 Nov 1994 08:49:37 GMT
ISO 8601 (almost)s1993-02-14T13:10:30
Universal Sortableu
1
2004-04-30 04:30:00Z
If there is, then you can use ParseExact with the single-character format specifier like this:
1
2
// 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, ddddCurrent 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, hhHours, 12 hour format.
H, HHHours, 24 hour format
m, mmMinutes
M, MM, MMM, MMMMMonth. MM = 2 digit, MMM = abbreviation (ie, ‘Jan’), MMMM=Spelled out.
s, ssSeconds
t, ttAM/PM indicator.
y, yy, yyyyYear
z, zz, zzzTime zone offset from GMT.
:Time separator.
/Date separator.
Any other characterOther characters are matched as literals.
So to parse our sample date, you’d use this string:
1
2
// 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:
1
2
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.

0 comments: