-1

I've got a string in this format:

21‎-‎10‎-‎2014‎ ‎15‎:‎40‎:‎30

I want to make it a DateTime in the format:

2014-‎10‎-‎21 ‎15‎:‎40‎:‎30

I have tried:

DateTime dt = DateTime.ParseExact("21‎-‎10‎-‎2014‎ ‎15‎:‎40‎:‎30", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

But no luck, it throws an exception String was not recognized as a valid DateTime

EDIT

I have also tried:

DateTime dt = DateTime.ParseExact("21-‎10‎-‎2014‎ ‎15‎:‎40‎:‎30", "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);

To have the same format in both parameters. The exception is the same, so the problem isn't a difference between formats. I have checked that before.

chiapa
  • 4,362
  • 11
  • 66
  • 106
  • 5
    The `format` parameter is not the format you _want_ the date to be in, but the format it _is_ in. Please use the search. – CodeCaster Jan 27 '15 at 10:42
  • 1
    To clarify CodeCaster's comment, the format for `Parse` needs to be the format it *is* in and the format for `ToString` the format you *want*. – CodesInChaos Jan 27 '15 at 10:44
  • 1
    "a DateTime in the format" is a non-sequitur. It's like asking for "an int in hex". A `DateTime` is just a value without a specific format... – Jon Skeet Jan 27 '15 at 10:47
  • See [duplicate](http://stackoverflow.com/questions/5366285/parse-string-to-datetime-in-c-sharp), [MSDN: Custom Date and Time Format Strings](https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx) and [C# DateTime to “YYYYMMDDHHMMSS” format](http://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format). – CodeCaster Jan 27 '15 at 10:49
  • @CodeCaster, that's not the same problem, I've checked that before – chiapa Jan 27 '15 at 10:52
  • Yes, it is. Read my first comment and the [`DateTime.ParseExact()`](https://msdn.microsoft.com/en-us/library/w2sa9yss%28v=vs.110%29.aspx) documentation. The format you specify as second parameter must match the format of the datetime string you pass as first parameter. Then you get a DateTime object, which you can print in any format you like. – CodeCaster Jan 27 '15 at 10:54
  • But as I commented below, the problem subsists with the same format, therefore, the issue isn't related to different formats. See my updated question – chiapa Jan 27 '15 at 10:58
  • Let me guess. You copied your `21‎-‎10‎-‎2014‎ ‎15‎:‎40‎:‎30` from somewhere else, right? – Soner Gönül Jan 27 '15 at 11:10
  • No, it comes from a string parameter – chiapa Jan 27 '15 at 11:11
  • i have no idea why this was down voted. seems like a legit question to me. – pgee70 Aug 13 '16 at 05:50
  • @pgee70, yet to find the downvotes reason. The solution involves the "invisible character" problem, which I don't think is very straightforward to understand or know. But hey, vigilantes be vigilantes and trigger happy on the downvote button – chiapa Sep 21 '16 at 09:32

1 Answers1

6

Your input string contains left-to-right mark characters, which you can see by pasting it in an Unicode-aware editor and viewing whitespace characters. See also Ideone (only visible in edit mode).

Clean up your input:

using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class Test
{
    public static void Main()
    {
        string dateTimeString = "21‎-‎10‎-‎2014‎ ‎15‎:‎40‎:‎30";
        dateTimeString = Regex.Replace(dateTimeString, @"[^\u0000-\u007F]", string.Empty);

        string inputFormat = "dd-MM-yyyy HH:mm:ss";
        string outputFormat = "yyyy-MM-dd HH:mm:ss";
        var dateTime = DateTime.ParseExact(dateTimeString, inputFormat, CultureInfo.InvariantCulture);
        string output = dateTime.ToString(outputFormat);

        Console.WriteLine(output);
    }
}

Output:

2014-10-21 15:40:30
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Upvoted. You are the only one who mentioned the invisible character problem rather than just mentioned _hey, your string and format doesn't match exactly_. – Soner Gönül Jan 27 '15 at 11:10
  • @Soner thanks, but I did so in comments and closed as duplicate before realizing. – CodeCaster Jan 27 '15 at 11:11
  • Thanks CodeCaster, the problem wasn't as simple as format differences. To post a question like that would be stupid. I'm not aware of these invisible characters you mention but your solution works indeed – chiapa Jan 27 '15 at 11:14
  • @chiapa _"To post a question like that would be stupid"_ - you don't say, yet they get asked multiple times a day. Glad to help. – CodeCaster Jan 27 '15 at 11:15
  • CodeCaster, the string comes from a javascript date that is added to an array and passed to a C# method. So, since you mentioned invisible chars, I'm guessing the problem could be the white space between the date and the time not being and actual white space? – chiapa Jan 27 '15 at 11:20
  • @chiapa see [this Ideone link](http://ideone.com/fork/IcO7Nx) to see the left-to-right characters. – CodeCaster Jan 27 '15 at 11:21
  • But, how did they end up there? – chiapa Jan 27 '15 at 12:01
  • @chiapa I don't know. They are in your post, so they must be in your variables, so the javascript you're reading it from must contain them too. – CodeCaster Jan 27 '15 at 12:02