1

I'm pulling a date/time from a MS SQL Server 2008 db and trying to format the date to show just the date in "dd/MM/yyyy" format.

The data in the DB looks like this:

2011-05-04 15:50:00.000

The unformatted string when displayed appears as this:

5/25/2011 8:47:00 AM

Yet this code fails when I try to parse it to the correct format:

DateTime dateA = DateTime.ParseExact(curShopDate, "ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture);
curShopDate = dateA.ToString();

I also tried this code, trying to split just the date portion away from the time:

string[] stringA = curShopDate.Split(' ');
DateTime dateA = DateTime.ParseExact(stringA[0], "ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture);
curShopDate = dateA.ToString();

Both versions crashed with an "String was not recognized as a valid DateTime." error.

2
  • the datetime examples are from different records. I didn't have the energy to dig through thousands of unsorted fields in the DB to find matching examples. Commented Dec 12, 2011 at 0:59
  • And....I'm stupid apparently. Solved it myself by pulling my head out of my arse and realizing it's already formatted and just stripping the time out. Commented Dec 12, 2011 at 1:10

3 Answers 3

4

The issue is with your format parameter. Your string is not in the ddMMyyyy format, it's in the M/dd/yyyy format:

string curShopDate = "5/25/2011 8:47:00 AM";
DateTime dateA = DateTime.ParseExact(curShopDate.Split(' ')[0], "M/dd/yyyy",
                              System.Globalization.CultureInfo.InvariantCulture);

You could also parse the string without stripping the time from the date:

string curShopDate = "5/25/2011 8:47:00 AM";
DateTime dateA = DateTime.ParseExact(curShopDate, "M/dd/yyyy h:mm:ss tt", 
                              System.Globalization.CultureInfo.InvariantCulture);
Sign up to request clarification or add additional context in comments.

Comments

2

According to MSDN:

The DateTime.ParseExact(String, String, IFormatProvider) method parses the string representation of a date, which must be in the format defined by the format parameter. It also requires that the and elements of the string representation of a date and time appear in the order specified by format, and that s have no white space other than that permitted by format.

So, if I'm reading that correctly, you specified the format as "ddMMyyyy" but your string is in "M/dd/yyyy h:mm:ss tt". Try either changing your format to "M/dd/yyyy h:mm:ss tt" or switch to DateTime.TryParse().

Comments

0

If you use ParseExact, then you must specify the exact format: "M/d/yyyy h:mm:ss tt", which matches your date string "5/25/2011 8:47:00 AM".

You can take the date component of a date/time with:

DateTime dateTime = DateTime.Now;
DateTime dateOnly = dateTime.Date;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.