1

I'm trying to parse 09/01/2015 00:00:00 to the format yyyy-MM-ddThh:mm:ssZ using following method:

DateTime.ParseExact("09/01/2015 00:00:00", "yyyy-MM-ddThh:mm:ssZ", (IFormatProvider)CultureInfo.InvariantCulture);

But I'm getting String was not recognized as a valid DateTime

Can anyone tell me why? I believe 09/01/2015 00:00:00 is a valid DateTime format?

Ozkan
  • 3,880
  • 9
  • 47
  • 78
  • 5
    Possible duplicate of [Converting a String to DateTime](http://stackoverflow.com/questions/919244/converting-a-string-to-datetime) – MakePeaceGreatAgain Jan 27 '16 at 14:12
  • 1
    When using ParseExtact the format-param has to match the inout string exactly, which is definitly not the case on your code. – MakePeaceGreatAgain Jan 27 '16 at 14:14
  • 2
    Do you seriously not see the issue with trying to parse `"09/01/2015 00:00:00"` with the format `"yyyy-MM-ddThh:mm:ssZ"`? The idea is that the format must look like the string. – Enigmativity Jan 27 '16 at 14:14
  • @HimBromBeere - I don't think that this is a duplicate. The OP clearly understands to use `ParseExact` (which the linked question provides), but he doesn't understand how to structure the format. – Enigmativity Jan 27 '16 at 14:21
  • possible dublicate of http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – Aishwarya Taneja Jan 27 '16 at 14:22

6 Answers6

2

From DateTime.ParseExact

Converts the specified string representation of a date and time to its DateTime equivalent. The format of the string representation must match a specified format exactly or an exception is thrown.

In your case, they are not.

I assume your 09 part is day numbers, you can use dd/MM/yyyy HH:mm:ss format instead.

var dt = DateTime.ParseExact("09/01/2015 00:00:00", 
                             "dd/MM/yyyy HH:mm:ss", 
                             CultureInfo.InvariantCulture);

Since CultureInfo already implements IFormatProvider, you don't need to explicitly cast it.

I don't understand this. So it means I first have to correct my string and secondly I can do a ParseExact(). I thought ParseExact could handle the given string...

ParseExact is not a magical method that can parse any formatted string you suplied. It can handle only if your string and format perfectly matches based on culture settings you used.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

Try this code:

var text = "09/01/2015 00:00:00";
var format = "dd/MM/yyyy HH:mm:ss";
var dt = DateTime.ParseExact(text, format, (IFormatProvider)CultureInfo.InvariantCulture);

You'll notice that the format must structurally match the text you're trying to parse exactly - hence the ParseExact name for the method.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
0

The format does not match, you need to change 09/01/2015 into 2015-01-09 or theyyyy-MM-dd part into dd/MM/yyyy.

GuyVdN
  • 690
  • 4
  • 14
  • I don't understand this. So it means I first have to correct my string and secondly I can do a ParseExact(). I thought ParseExact could handle the given string... – Ozkan Jan 27 '16 at 14:17
  • @Ozkan No, the format and string must match, exactly like the error message says. They don't match, you get an exception. – mason Jan 27 '16 at 14:20
0

The ParseExact-method is no ultimate method that converts ANY dateformat into another one, it is simply to parse a given string into a datetime using the provided format. Thus if your inout does not match this format the method will throw that exception.

As a datetime is internally only a number there is no need to convert one format into another at all, so as long as you know your input-format you can build a date from it which has nothing to do with any formatting which you may need when you want to print that date to your output. In this case you WILL need a formatter.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
0

As most people have stated the error is coming from the fact that the date in string format doesn't match the format you are saying it's in. You are saying that 09/01/2015 00:00:00 is in the format "yyyy-MM-ddThh:mm:ssZ", which it's not, hence the error. To rectify this you need to either alter the format the string is in, or more likely, change the format you are saying the date is in. So change "yyyy-MM-ddThh:mm:ssZ" to "dd/MM/yyyy HH:mm:ss".

In a more long term view how are you arriving at that date? Is it possible that the format may change (input but the user)? If so it might be better to try and avoid the error being thrown and handle it better with TryParseExact. To make use of this best I generally output a nullable DateTime and then check if it's null. If you don't do this then if the parse fails it will simply make the output datetime the minimum value.

Something like this should work:

public DateTime? StringToDate (string dateString, string dateFormat)
{
    DateTime? dt;
    DateTime.TryParseExact(dateString, dateFormat, null, System.Globalization.DateTimeStyles.None, out dt);
    return dt;
}

Then you can use it like this:

DateTime? MyDateTime = StringToDate("09/01/2015 00:00:00", "dd/MM/yyyy HH:mm:ss");

if(MyDateTime != null)
{
    //do something
}
sr28
  • 4,728
  • 5
  • 36
  • 67
0

Another simple way to do this...

var dt = Convert.ToDateTime(Convert.ToDateTime("09/01/2015 00:00:00").ToString("yyyy-MM-ddThh:mm:ssZ"))