13

I have a string like this: 250920111414

I want to create a DateTime object from that string. As of now, I use substring and do it like this:

string date = 250920111414;

int year = Convert.ToInt32(date.Substring(4, 4));
int month = Convert.ToInt32(date.Substring(2, 2));
...
DateTime dt = new DateTime(year, month, day ...);

Is it possible to use string format, to do the same, without substring?

hogni89
  • 1,920
  • 6
  • 22
  • 39

3 Answers3

25

Absolutely. Guessing the format from your string, you can use ParseExact

string format = "ddMMyyyyHHmm";

DateTime dt = DateTime.ParseExact(value, format, CultureInfo.InvariantCulture);

or TryParseExact:

DateTime dt;
bool success = DateTime.TryParseExact(value, format, 
                     CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);

The latter call will simply return false on parse failure, instead of throwing an exception - if you may have bad data which shouldn't cause the overall task to fail (e.g. it's user input, and you just want to prompt them) then this is a better call to use.

EDIT: For more details about the format string details, see "Custom Date and Time Format Strings" in MSDN.

Justin
  • 6,611
  • 3
  • 36
  • 57
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 4
    Do you really remember all those simple but useful solutions by heart or you have a big big big dictionary near your hand? :) – Tadeusz Sep 28 '11 at 09:09
  • 1
    @Praetor12: For basics like DateTime.ParseExact I remember the method names, but sometimes get the parameters wrong - as I did here, first time :) – Jon Skeet Sep 28 '11 at 09:10
  • Jon Skeet is simply awesome! Sometimes I feel like having a +1000 button as well. – Kangkan Sep 28 '11 at 09:16
  • @Jon Skeet I told not about methods names, but about format strings. In C# there are so many "formats". May be there is a total list of them. Msdn has only sparse examples in different articles :( – Tadeusz Sep 28 '11 at 09:17
  • @Praetor12: Ah, that wasn't clear from your previous comment. I'm pretty familiar with the format strings, especially as I'm currently implementing text parsing/formatting for Noda Time. But in terms of MSDN, look at "Custom Date and Time Format Strings" - I'll add a link in the answer. – Jon Skeet Sep 28 '11 at 09:48
  • 1
    Sometimes stackoverflow gives me that warm & cozy feeling. Like when I need to find info on reversing a custom DateTime.ToString() and I know that Jon Skeet will have answered it. And he did... – Zeph Mar 16 '12 at 16:07
  • How do you handle this problem adding milliseconds/timezone to the end? I can't get ParseExact to do this. – Chazt3n Mar 03 '14 at 18:16
  • @Chazt3n: Well for milliseconds you just use `fff` or `FFF` depending on exactly what you want. For "time zone" you'd need to be more precise about exactly what you mean - a time zone or an offset? Have you read the "custom date and time format strings" page I link to in the answer? – Jon Skeet Mar 03 '14 at 18:25
  • I did, and it still didn't work! It was an issue in LinqPad, and by creating a new file and copy/pasting the old code, it magically worked. Thank you and I should have caught on sooner. – Chazt3n Mar 04 '14 at 18:45
4

You could use:

DateTime dt = DateTime.ParseExact(
                  date, 
                  "ddMMyyyyHHmm",
                  CultureInfo.InvariantCulture);
Marco
  • 56,740
  • 14
  • 129
  • 152
0
string iDate = "05/05/2005";
DateTime oDate = Convert.ToDateTime(iDate);
DateTime oDate = DateTime.ParseExact(iString, "yyyy-MM-dd HH:mm tt",null);


DateTime Formats