Click here to Skip to main content
12,756,705 members (29,868 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
i need to compare the date of system by the date of selected date in monthcalender in c#
how can i write the code
if (monthCalendar1.SelectionRange.Start.ToString()= DateTime.Now) ;
what is write code to
Posted 19-Nov-12 10:12am
Sergey Alexandrovich Kryukov 19-Nov-12 15:38pm
Looks like not a question. Maybe, incomplete question...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Compare the dates (or, more generally, points of time in the form of System.DateTime using the operators: "==", ">", "<", ">=" and ">=". Also, use "-" (subtraction) to find the time interval, duration.

System.DateTime before = //...
System.DateTime after = //...


if (before == after) { /* same time */ }
if (after >= before) { /* as expected */ }


if (after < before) { /* something's wrong */ }

System.TimeSpan duration = after - before; // as simple as that! now see how much time elapsed

Please see:[^],[^].

Espen Harlinn 28-Nov-12 18:15pm
Nice reply :-D
Sergey Alexandrovich Kryukov 28-Nov-12 19:31pm
Thank you, Espen.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Don't do ToString. why on earth would you ? A string comparison will not correctly compare dates. DateTime.Now is NOW. An hour from how is greater, an hour less is less. Remember that. Apart from that, if you remove the ToString, I bet you're getting a DateTime and this would work.
meme_307 20-Nov-12 9:55am
thanks * i made date instead of to string and it works good

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170217.1 | Last Updated 28 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100