7

When I try to parse date like this:

DateTime t1 = DateTime.ParseExact("August 11, 2013, 11:00:00 PM", "MMMM dd, yyyy, hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);

It works correctly but when I do thing like this :

string s ="‎August ‎11, ‎2013, ‏‎11:00:00 PM";
DateTime t = DateTime.ParseExact(s, "MMMM dd, yyyy, hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);

I get this error :

An exception of type 'System.FormatException' occurred in mscorlib.ni.dll but was not handled in user code

4 Answers 4

12

Because your string

string s = "‎August ‎11, ‎2013, ‏‎11:00:00 PM";

Includes 0x200e(8206) character at the beginning and end of August. You can see it easily by

var chars = s.ToCharArray();

Seems to be a copy+paste problem

You can remove those chars by:

var newstr = new string(s.Where(c => c <128).ToArray())
3
  • so how can i remove it as i already get it from sting in model?
    – Ahmed Emad
    Aug 18, 2013 at 11:03
  • 2
    @AhmedEmad var newstr = new string(s1.Where(c => c <128).ToArray())
    – I4V
    Aug 18, 2013 at 11:06
  • @AhmedEmad The reason why the left-to-right and right-to-left marks are there is probably that the string comes from some Arabic text with English month name and left-to-right ordered digit groups in it. You can remove the non-ASCII chars like in the answer, but if they are always there in the same place, why not just parse them, as in DateTime.ParseExact(s, "\u200EMMMM \u200Edd, \u200Eyyyy, \u200F\u200Ehh:mm:ss tt", CultureInfo.InvariantCulture). Aug 19, 2013 at 19:23
3

Haha, I found it.

First of all, there is nothing wrong with both of your code. Both works fine. Just your strings are not equal. There are some hidden characters on your second one.

Your first "August 11, 2013, 11:00:00 PM".Length is 28

but second "‎August ‎11, ‎2013, ‏‎11:00:00 PM".Length is 33

Let's try this code;

string s = "August 11, 2013, 11:00:00 PM";
string s1 = "‎August ‎11, ‎2013, ‏‎11:00:00 PM";

char[] c = s.ToCharArray();
char[] c1 = s1.ToCharArray();

foreach (var ch in c)
{
    Console.WriteLine(ch);
}

foreach (var ch1 in c1)
{
    Console.WriteLine(ch1);
}

Output will be;

A
u
g
u
s
t

1
1
,

2
0
1
3
,

1
1
:
0
0
:
0
0

P
M
? // <-- What the hell?
A
u
g
u
s
t

? // <-- What the hell?
1
1
,

? // <-- What the hell?
2
0
1
3
,

? // <-- What the hell?
? // <-- What the hell?
1
1
:
0
0
:
0
0

P
M

As a solution, don't copy paste any string to your code :).

3
  • @Downvoter care to comment at least so I can see where I might be wrong? Oct 10, 2017 at 8:02
  • You realize you misspelled length every time, right? :)
    – SmartDev
    May 28, 2020 at 7:30
  • @SmartDev Ups, 7 years old mistake, fixed (: May 28, 2020 at 10:59
2

Your second string has hidden characters.

Run this:

string s1 = "August 11, 2013, 11:00:00 PM";
string s2 = "‎August ‎11, ‎2013, ‏‎11:00:00 PM";

Console.WriteLine(s1.Length); // 28
Console.WriteLine(s2.Length); // 33

Specifically, as char arrays, the second one is this:

s2.ToCharArray();
{char[33]}
[0]: 8206 '‎' // ????
[1]: 65 'A'
[2]: 117 'u'
[3]: 103 'g'
[4]: 117 'u'
[5]: 115 's'
[6]: 116 't'
[7]: 32 ' '
[8]: 8206 '‎' // ????
[9]: 49 '1'
[10]: 49 '1'
[11]: 44 ','
[12]: 32 ' '
[13]: 8206 '‎' // ????
[14]: 50 '2'
[15]: 48 '0'
[16]: 49 '1'
[17]: 51 '3'
[18]: 44 ','
[19]: 32 ' '
[20]: 8207 '‏' // ????
[21]: 8206 '‎' // ????
[22]: 49 '1'
[23]: 49 '1'
[24]: 58 ':'
[25]: 48 '0'
[26]: 48 '0'
[27]: 58 ':'
[28]: 48 '0'
[29]: 48 '0'
[30]: 32 ' '
[31]: 80 'P'
[32]: 77 'M'
2

I was hit with this too. In my case, a UI automation test failed because IE seems to add this LRM (Left-to-right) mark automatically (Firefox and Chrome do not). A quick line of code that strips it away:

Regex.Replace(date, @"\u200e", string.Empty)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.