Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 22-Mar-12 22:13pm
Edited 22-Mar-12 23:36pm
v2
Comments
ProEnggSoft at 23-Mar-12 5:36am
   
Edit: pre tag for C# code added and question tagged sith ASP.NET and C# - PES
shek124 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You could do it in this way:
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");
}
  Permalink  
Comments
deepu kuamar at 23-Mar-12 6:19am
   
DateTime dtSuppliedDate = DateTime.Parse(txtDate.Text);///It through error
String was not recognized as a valid DateTime.
Praveen Kullu at 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 at 23-Mar-12 7:36am
   
Write the date you are entering in reply Commments. Probably the format of the date is wrong.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Please try Google. There are many postings on many sites for comparing dates in C#, ASP.NET.
 

Hope this help.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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. 
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Hi,
 

Use This One
 
 
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...
  Permalink  
Comments
Member 9550455 at 1-Dec-12 4:12am
   
nice one. thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

----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;
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

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[^]
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 9 Jun 2014
Copyright © CodeProject, 1999-2014
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