Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can anybody tell me what wrong with this code

C#
string s =@"­";  //s = "-";
string r = HttpUtility.HtmlDecode(s);
string r3 = string.Format("1{0}Jan{0}2007",r);

string format = "d-MMM-yyyy";
DateTime MinedDateTime ; 
bool result = DateTime.TryParseExact(r3.Trim(), format, null, DateTimeStyles.None, out MinedDateTime);
Console.WriteLine(r3);

if(r3 == "1-Jan-2007")
{
	Console.WriteLine("Equal");
	Console.WriteLine("1-Jan-2007");
}
else
{
	Console.WriteLine("Not Equal");
	Console.WriteLine("1-Jan-2007");
}
Console.WriteLine(result);


the Output is
1-Jan-2007
Not Equal
1-Jan-2007
False



but when i replace
string.Format("1{0}Jan{0}2007",r);
with
string.Format("1{0}Jan{0}2007","-");
every thing works fine

the Output is
1-Jan-2007
Equal
1-Jan-2007
True

s= html code of '-'(hypen) is not showing
Posted
Updated 6-Feb-13 17:50pm
v2

1 solution

You are doing one of the worse of the very usual mistakes of beginners this days: trying to work with string representation of data instead of data.

Isn't this obvious: two points of times showing identical time or identical data are still identical, even if they are shown in different string representations, due to d
C#
ifferent formats/cultures? Compare time, not strings:
System.DateTime thisInstance = System.DateTime.Now;
System.DateTime someOtherTime = // .. something else
if (thisInstance == someOtherTime) // ... you can use equivalence operator
// or
if (thisInstance >= someOtherTime) // ... and so on


Why, why comparing strings representing time or date, ever?

You need those strings, for example, when you need to show them on screen.

First, learn about all System.DateTime.ToString methods: http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

Second, learn formatting:
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx[^],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

That's all.

—SA
 
Share this answer
 
Comments
agha_ali22 7-Feb-13 1:32am    
the problem is why r3 and string literal "1-Jan-2007" are not equal. Is there an Encoding issue because when i DateTime.TryParseExact("1-Jan-2007", format, null....
it parses the date correct when when i use r3 the problem occurs
Sergey Alexandrovich Kryukov 7-Feb-13 1:39am    
No, your problem is wrong approach. I told you all: what to do and how to format strings. Need parsing? Hardly, but then read help on the method Parse, ParseExact, TryParse, TryParseExact. What's the problem. I gave you the reference.
—SA
agha_ali22 7-Feb-13 2:03am    
i am still stuck at this please anwser only this why r3 and "1-Jan-2007" are not equal because when i print the two both has same value
in case you miss s = is html code of '-'(hypen) is not showing
Sergey Alexandrovich Kryukov 7-Feb-13 8:40am    
You will show what you want without any limitation is you use custom DateTime format. I gave you a reference above.
—SA
agha_ali22 8-Feb-13 11:30am    
i fixed the issue by replacing SOFT HYPHEN to HYPHEN-MINUS thanks for giving me the hint "...... trying to work with string representation of data instead of data. "

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900