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
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())
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 :).
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'
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With