1

I have a date which comes in a string like so:

09/25/2014 09:18:24

I need it like this (yyyy-mm-dd):

2014-09-25 09:18:24

The object that this date goes into is a nullable date.

Tried this does not work:

DateTime formattedDate;
bool result = DateTime.TryParseExact(modifiedDate, "yyyy-MM-dd",
               CultureInfo.InvariantCulture,
               DateTimeStyles.None,
               out formattedDate);

Any clues?

Thanks in advance.

6
  • possible duplicate of Converting String to DateTime C#.net Commented Nov 4, 2014 at 11:15
  • Are you confusing fornmatting and parsing? If the input-string is 09/25/2014 why do you parse it with yyyy-MM-dd? Commented Nov 4, 2014 at 11:17
  • You also seem to be confused in terms of what a DateTime stores... it doesn't have a format... it's just a date and time. See stackoverflow.com/questions/9763278 Commented Nov 4, 2014 at 11:19
  • Alright, all three are good suggestions but I need to store it in SQL which takes this 2014-09-25 09:18:24 Commented Nov 4, 2014 at 11:33
  • @Codehelp What is the column type that you want to insert this DateTime? Isn't that a character type I hope.. Commented Nov 4, 2014 at 11:36

3 Answers 3

4

From DateTime.TryParseExact

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.

In your case, they are not. Use yyyy-MM-dd HH:mm:ss format instead.

string s = "2014-09-25 09:18:24";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyy-MM-dd HH:mm:ss",
                          CultureInfo.InvariantCulture,
                          DateTimeStyles.None, out dt))
{
    Console.WriteLine(dt);
}

It is a little bit unclear but if your string is 09/25/2014 09:18:24, then you can use MM/dd/yyyy HH:mm:ss format instead. Just a tip, "/" custom format specifier has a special meaning as replace me with current culture or supplied culture date separator. That means, if your CurrentCulture or supplied culture's DateSeparator is not /, your parsing operation will fail even if your format and string matches exactly.

If you have already a DateTime and you want to format it, you can use DateTime.ToString(string) method like;

dt.ToString("yyyy-mm-dd", CultureInfo.InvariantCulture); // 2014-09-25

or

dt.ToString("yyyy-mm-dd HH:mm:ss", CultureInfo.InvariantCulture); // 2014-09-25 09:18:24

Remember, a DateTime does not have any implicit format. It just contains date and time values. String representations of them have formats.

Sign up to request clarification or add additional context in comments.

4 Comments

Does not work. TryParseExact fails. Maybe something to do with the slashes not being in the input string. No?
@Codehelp Which one doesn't work? What is your modifiedDate exactly? You have an already a DateTime and you want to format it or your have a string with specific format that you want to parse it? Yes / character has a special meaning as I said in my answer but since you use InvariantCulture, this is not a problem.
Ok, tried Stefano's answer, converted it to DateTime and then tried your ToString. It's still the same that goes in.
@Codehelp What is still the same that goes in exactly? Please be more specific. I told you in my answer as It is a little bit unclear but if your string is 09/25/2014 09:18:24, then you can use MM/dd/yyyy HH:mm:ss format instead. With .ToString() method, you have a string representations of your DateTime with a specific format. And now, you have a problem with try to insert this string value to your database? What is the column type that you want to keep it? And how do you insert it anyway?
1

In answer to your question, to convert it as you prefer, do it like this:

string originalDate = "09/25/2014 09:18:24";

DateTime formattedDate;

if (DateTime.TryParseExact(originalDate, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out formattedDate))
{
    string output = formattedDate.ToString("yyyy-mm-dd HH:mm:ss", CultureInfo.InvariantCulture);
}

And then output will have your desired format.

Comments

0
DateTime  dateOf = Convert.ToDateTime("09/25/2014 09:18:24");
string myFormat = "yyyy-mm-dd";
string myDate = dateOf.ToString(myFormat);  // output 2014-18-25 

Datetime format

2 Comments

Why are you using ddd, MMM and d formats anyway? They are irrelevant with question.
Still, there is no guarantee that your CurrentCulture parses your MM/dd/yyyy HH:mm:ss formated string since you use Convert.ToDateTime(string) method without any IFormatProvider.

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.