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.

Codehelp
  • 4,157
  • 9
  • 59
  • 96
  • possible duplicate of [Converting String to DateTime C#.net](http://stackoverflow.com/questions/919244/converting-string-to-datetime-c-net) – Emre Acar Nov 04 '14 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`? – Tim Schmelter Nov 04 '14 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 http://stackoverflow.com/questions/9763278 – Jon Skeet Nov 04 '14 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 – Codehelp Nov 04 '14 at 11:33
  • @Codehelp What is the column type that you want to insert this `DateTime`? Isn't that a character type I hope.. – Soner Gönül Nov 04 '14 at 11:36
  • @Codehelp I edit my answer, now you should have the correct format for your DB, try it :) – Stefano Bafaro Nov 04 '14 at 11:46

3 Answers3

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Does not work. TryParseExact fails. Maybe something to do with the slashes not being in the input string. No? – Codehelp Nov 04 '14 at 11:24
  • @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. – Soner Gönül Nov 04 '14 at 11:28
  • Ok, tried Stefano's answer, converted it to DateTime and then tried your ToString. It's still the same that goes in. – Codehelp Nov 04 '14 at 11:31
  • @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? – Soner Gönül Nov 04 '14 at 11:42
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.

Stefano Bafaro
  • 915
  • 10
  • 20
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

Satinder singh
  • 10,100
  • 16
  • 60
  • 102
  • Why are you using `ddd`, `MMM` and `d` formats anyway? They are irrelevant with question. – Soner Gönül Nov 04 '14 at 11:35
  • 1
    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`. – Soner Gönül Nov 04 '14 at 11:44