-2

I need to compare two date format strings:

dateString in "dd-MMM-yy" format

with

referenceDateString in "M/dd/yyyy hh:mm:ss tt" format respectively.

For that, I need to convert the dateString = "dd-MMM-yy" to "M/dd/yyyy hh:mm:ss tt".

However, Got an error while trying to do that:

"Error: string was not recognized as a valid datetime".

The C# code I used given below.

string dateString = "19-Dec-14";
string AsofDate = DateTime.ParseExact(dateString, "M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

Edit 1:

In the actual code the dateString obtaining after reading a csv file which is supplied as "19-Dec-14", that's why it's in the string format.

Please help, am pretty new to C#. Thanks.

JCurious
  • 11
  • 1
  • 1
  • 4
  • 2
    For parsing you will need `"dd-MMM-yy"` as you specified in your question. Does your format `"M/dd/yyyy hh:mm:ss tt"` *remotely* matches your `dateString` `"19-Dec-14"` ? – Habib Mar 23 '15 at 12:59
  • 2
    Later use the format `"M/dd/yyyy hh:mm:ss tt"` in `ToString` on parsed `DateTime` object. – Habib Mar 23 '15 at 13:00
  • You should not worry about comparing *strings*, but just compare the `DateTime` objects. This means you only have to worry about parsing, and to parse you need to use the format that matches the string (see Habib's comment). – crashmstr Mar 23 '15 at 13:03
  • imho there is enough information on the internet about formatting. The amount of questions still is overwhelming. People google is your friend. – Philip Stuyck Mar 23 '15 at 13:04
  • To be precise, I need to convert the 19-Dec-14 to 12/19/2014 12:00:00 AM ... – JCurious Mar 23 '15 at 13:10

2 Answers2

4

Habib already gave the answer on his comments, I try to add it as an answer;

From DateTime.ParseExact(String, String, IFormatProvider)

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

In your case, clearly they don't. First, you need to parse your string to DateTime with proper format (which is dd-MMM-yy with an english-based culture), then you can get the string represention of your DateTime with specific format.

string s = "19-Dec-14";
DateTime dt;
if(DateTime.TryParseExact(s, "dd-MMM-yy", CultureInfo.InvariantCulture,
                          DateTimeStyles.None, out dt))
{
    dt.ToString("M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture).Dump();
    // Result will be 12/19/2014 12:00:00 AM
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

It's not entirely clear what you are trying to do, but in order to parse that date you have on the first line, you would use something like this:

DateTime AsofDate = DateTime.ParseExact(dateString, "dd-MMM-yy", System.Globalization.CultureInfo.InvariantCulture);

Note a couple things here: I've changed the data type of AsofDate from string to DateTime because that's what DateTime.ParseExact returns. Also, I've modified the custom format string to match the format of the string you are trying to parse as a date ("19-Dec-14").

rory.ap
  • 34,009
  • 10
  • 83
  • 174