Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string todaydate = DateTime.Now.ToString("dd/MM/yyyy");
if(txtDate.text<todaydate)>
{
    Response.Write("Supplied date is not Current Date");
}

Why it is not except operator < or >. I m not able to understand
Thanks
Posted
Updated 22-Mar-12 23:36pm
v2
Comments
ProEnggSoft 23-Mar-12 5:36am    
Edit: pre tag for C# code added and question tagged sith ASP.NET and C# - PES
shek124 23-Mar-12 6:02am    
you cannot compare the strings using comparison operators.
Please have to go through the basic things. Then you start to write the program/code.

Do not work with date strings, work with the instance of the structure System.DateTime. Those strings should appear only when you about to display data on screen (in HTML, for most typical case).

The instances of DataTime can be compared with operators '==', '!=', '<', '>', '<=' or '>='.
You can also subtract two points in time to get an instance of the structure System.TimeSpan representing the distance in time between them. The most readable way of doing such subtraction is, no surprise, the operator '-' (minus). If you subtract earlier time from later time the result is positive, negative in the opposite case.

Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^].

—SA
 
Share this answer
 
v2
You could do it in this way:
C#
string todaydate = DateTime.Now.ToString("dd/MM/yyyy");
DateTime dtSuppliedDate = DateTime.Parse(txtDate.Text);

if(dtSuppliedDate.Subtract(DateTime.Today).Days!=0)
{
    Response.Write("Supplied date is not Current Date");
}
 
Share this answer
 
Comments
deepu kuamar 23-Mar-12 6:19am    
DateTime dtSuppliedDate = DateTime.Parse(txtDate.Text);///It through error
String was not recognized as a valid DateTime.
Praveen Kullu 23-Mar-12 7:16am    
Show the exact text you are entering in txtDate. If it is not in valid date format, it will show error.
Praveen Kullu 23-Mar-12 7:36am    
Write the date you are entering in reply Commments. Probably the format of the date is wrong.
Please try Google. There are many postings on many sites for comparing dates in C#, ASP.NET.


Hope this help.
 
Share this answer
 
Hi,

You can not compare the date in this way , in current code you have compared
strings,You should first convert the textbox input in DateTime type then you can compare the dates

Thanks
 
Share this answer
 
DateTime systemDate = DateTime.Now;
DateTime compareDate = DateTime.Today.AddHours(11D);


// less than
if (compareDate < systemDate)
    Console.WriteLine("Less Than");


// equal to
if (compareDate == systemDate)
    Console.WriteLine("Equal To");


// greater than
if (compareDate > systemDate)
    Console.WriteLine("Greater Than");


// basically you can compare it in all the normal ways
// using !=, ==, <, >, <=, >=, etc


// or if you just want a straight number result inorder to sort
// you can use
int compareValue = compareDate.Compare(systemDate);
int compareValue2 = DateTime.Compare(systemDate, compareDate);



But remember it is always better to use a lighter weight process.  The Convert or Parse process is pretty heavy because it has to take the string break it apart compare it against a whole set of rules and then based on the rules it has to change them to the correct object.  Or with what I did above you are simply adding two decimal values together which is a much faster process. 
 
Share this answer
 
Hi,


Use This One

C#
int dtCmpr = Convert.ToInt32(DateTime.Compare(Convert.ToDateTime(DateTime.Now), Convert.ToDateTime(txtDate.text)));

if (dtCmpr <= -1)
{
    Response.Write("Supplied date is not Current Date");
}


Hope This May Help You...
 
Share this answer
 
Comments
Member 9550455 1-Dec-12 4:12am    
nice one. thanks
----Using Java Script----
function CheckDate() {
var SDate = document.getElementById("txtStartFrom");
var EDate = document.getElementById("txtValidUpto");

var endDate = new Date(EDate.value);
var startDate = new Date(SDate.value);
if (SDate != '' && EDate != '' && startDate > endDate) {
alert("Please ensure that the End Date is greater than or equal to the Start Date.");
EDate.focus();
EDate.value = SDate.value;
return false;
}
 
Share this answer
 
SQL
Maybe you can use ToString() method.  use this address to rearrange date values and

if (currentTime.ToString("yyyyMMdd")== endTime.ToString("yyyyMMdd")



pls check this link

http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm[^]
 
Share this answer
 
v2

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