views:

91

answers:

7

How do I convert a date string, in the general form of "ccyymmdd" in to a DateTime object in C#?

For example, how would I convert "20100715" in to a DateTime object.

Please - No RTFM links to Microsoft Tech Docs.

Many Thanks...

+3  A: 

Hi there.

var dt = DateTime.Parse("your date string").ToString("yymmdd");

I don't think cc is a valid date formatting option?

As Richard points out, you can also use DateTime.ParseExact which allows you to use culture information for the parsing, or you can use DateTime.TryParseExact which is the same as DateTime.ParseExact, but if there is an exception then a null date is returned rather then an exception being raised.

EDIT:

The question has been updated so that a DateTime is specifically returned. In that case you can omit the .ToString() part of my answer. Calling DateTime.Parse() will return a DateTime object. When getting the date value via ToString(), simply pass the required formatting string to get the date in the desired format. Cheers. Jas.

Jason Evans
It means century. That's most (if not all) the OP is asking about.
Noldorin
Thanks Jason. Yes I know 'cc' is suspect, I just use it to help differentiate between century and year within century.
logout
-1 for not addressing the real question.
Noldorin
+1 but @Jason Evans you may want to edit your answer to keep it a datetime object instead of converting it back to a string. I think thats what @Noldorin was getting at.
Gage
@Noldorin - Fair enough, I've shown him how to parse dates in .NET, but not how to include a century value. I've +1 you since, fair play, you have provided a more thorough solution.
Jason Evans
This is wrong, this will return another date String.
Nate Bross
@Jason: That's very honorable. If you flesh out the bit about `cc` not being valid in the string... and remove the ToString call, I can change the -1 to a +1 though, my votes are never immutable. :)
Noldorin
This doesn't work. DateTime.Parse() will not parse a string in the OP's stated date format. DateTime.Parse("20100715") throws a FormatException.
Mike Powell
Hah, I can't believe I've gone to much effort over nothing. `ccyy` is equivalent to `yyyy`, no rearranging necessary! Silly me.
Noldorin
A: 

Take a look at this and this

DateTime.Parse();
DateTime.ParseExact();

And worth a mention

DateTime.TryParse();
Richard Friend
A: 

Use the DateTime.Parse method to do the conversion

armannvg
A: 

System.DateTime.Parse(yourDateString)

You might have to manipulate your string to a format that the method can handle first.

See http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

for more info

Longball27
+1  A: 

If your date string is already sanitized (Borrowed from Mike's answer):

DateTime dt = DateTime.ParseExact("20100715", "yyyyMMdd", CultureInfo.InvariantCulture);

Otherwise:

DateTime dt;
if (!DateTime.TryParseExact("20100715", "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
    // Handle bad date
}
Phil
`cc` isn't a date-time formatting specifier in .NET.
Noldorin
it just means the 20 in 2010 (the OP means yyyymmdd)- a DateTime object will handle it just fine.
Phil
DateTime.Parse("20100715") throws System.FormatException.
Mike Powell
How does that code work?? And it doesn't answer the question
w69rdy
Why was this down-voted? This makes the most sense.
Nate Bross
He's changed it, it's ok now but it didnt work before. I removed my -1
w69rdy
I assume it was downvoted because it doesn't work. See my earlier comment.
Mike Powell
@Mike, you're right - I assumed it would work the same way as the SQL Server parser. The problem is more complicated than it first appears.
Phil
@w69rdy, I meant the ccmmddyy to represent the date string. Obviously confusing to everyone but me!
Phil
@Phil Yeah now you've changed it I can see what you meant!
w69rdy
Now with working-ness.
Phil
@Phil. Well done (for not letting "ccyy" throw you). I've tried your TryParseExact() - works a treat. Thanks again.
logout
A: 

I'm not sure what the "cc" part is, but there are a few options.

DateTime.Parse(string) may be able to convert the string, but if the string is in a non-standard format you may have to do some pre-conversion first.

Dave Swersky
cc will be 19 for 19xx years and 20 for 20xx years.
Phil
So an example would be 19700101 for January 1st 1970? That's almost certainly a non-standard string. You could try DateTime.Parse, and if it doesn't work you'd have to rework the string and *then* feed the sanitized string into DateTime.Parse.
Dave Swersky
@Dave: yyyyMMdd is about as standard as a date format gets. See http://en.wikipedia.org/wiki/ISO_8601#Calendar_dates
LukeH
+5  A: 
using System.Globalization;

DateTime.ParseExact("20100715", "yyyyMMdd", CultureInfo.InvariantCulture);
Mike Powell
Of all the answers I think this is the cleanest way to parse the date string.
Phil
Spot on Mike. Thanks v much. Youv'e saved what little hair I have left.
logout
No problem. We follically challenged dudes need to stick together. :)
Mike Powell
This is how I format my DateTimes too, and no sense answering the same solution. Good job Mike. +1
jlafay