9

When trying to use the parse method on the DateTime class, I get an exception thrown:

String was not recognized as a valid DateTime.

  • The string reads as "26/10/2009 8:47:39 AM" when outputted.
  • This string is obtained from a group on a match from a regex.
  • None of the strings obtained from this match group will parse to datetime.

Examples of other strings:

26/10/2009 8:47:39 AM
26/10/2009 8:00:41 AM
26/10/2009 7:48:35 AM

The weird thing is, I am sure it has worked before.

Andrew
  • 307
  • 7
  • 13
Matthew
  • 217
  • 1
  • 2
  • 9

7 Answers7

25

Parsing strings into DateTime object is almost always a pain. If you know for certain that they will always have the format as your examples do, this should work:

string input = "26/10/2009 8:00:41 AM";
DateTime dateTime = DateTime.ParseExact(input, "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • I was writing this exact same answer. – Vinko Vrsalovic Dec 08 '09 at 23:27
  • Although I was going to add some information about the regional settings (globalization) that affect date parsing. – Vinko Vrsalovic Dec 08 '09 at 23:29
  • @Vinko: so did I. It's the pain part. ;) No, seriously, it is a rather complex area that is a common problem source. I did write a sort of lengty answer on the topic a while ago here: http://stackoverflow.com/questions/1437454/date-format-problem – Fredrik Mörk Dec 08 '09 at 23:36
  • @FredrikMörk i´m using this solution, the strings input is correctly formated but my datetime variable is setting to 1/1/0001 12:00:00 AM, i´m using it in .Net 4.5, do you have any idea dude? – Guillermo Oramas R. May 07 '13 at 15:47
  • @Guillelon what does your input string and format string look like? – Fredrik Mörk May 07 '13 at 20:49
12

Parse takes regional settings (culture of current thread) into account. Therefore, I'd use ParseExact and specify the correct format explicitly with an invariant culture (or the culture you need, eg. en-US, for AM/PM).

Lucero
  • 59,176
  • 9
  • 122
  • 152
3

You're probably using the wrong culture. The month cannot be 26, so it's not a US timestamp. This works though:

using System;
using System.Globalization;

class Program
{
    static void Main(string[] args)
    {
        DateTime dateTime = DateTime.Parse("26/10/2009 8:47:39 AM",
            CultureInfo.GetCultureInfo("en-GB"));
    }
}
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
2

Has the culure changed on the machine? 26/10/2009 is a good UK date but a bad US date (for instance)

Pharabus
  • 6,081
  • 1
  • 26
  • 39
  • That had actually just occurred to me. Datetime.parse seems to have an overload for the cultural time format. Just trying to find out how to use it now :) Thanks for you help. – Matthew Dec 08 '09 at 23:28
2

Either call DateTime.Parse() with the culture as a parameter or call DateTime.ParseExact() with the date, the exact format of the date to parse, and the culture:

DateTime.ParseExact()

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
1

I second @Lucero, Parse uses current thread's culture info etc. See also the opposite direction: a ToString question in this context.

Community
  • 1
  • 1
Ron Klein
  • 9,178
  • 9
  • 55
  • 88
0

I have to solve some really strange date format coming from other system... In this example is for SPANISH format ...

string input="07/06/2019 07:01:54 p. m.";


public static DateTime ParseSpanishDate(string input)
{
    string normalized = input.Replace("a. m.", "AM").Replace("p. m.", "PM");
    normalized = normalized.Replace("12:00:00 AM", "00:00:00 AM");
    return  DateTime.Parse(normalized, CultureInfo.GetCultureInfo("es"));
}
MadMad666
  • 955
  • 3
  • 11
  • 19