Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
How to compare text box date with current date which is in dd-MMM-yyyy format.
Posted

Hi!

Try following code:

C#
DateTime dt1 = new DateTime.Now;
String.Format("{0:dd-MMM-yyyy}", dt1);
DateTime dt2 = Convert.ToDateTime(txtDate.text);
String.Format("{0:dd-MMM-yyyy}", dt1);
int result = string.Compare(dt1, dt2,true);
string relationship;
if (result < 0)
   relationship = "is earlier than";
else if (result == 0)
   relationship = "is the same time as";         
else
   relationship = "is later than";


Regards.
 
Share this answer
 
Comments
taha bahraminezhad Jooneghani 3-Jun-12 13:34pm    
this is much better than my solution, very good!
http://stackoverflow.com/questions/3509683/validate-two-dates-of-this-dd-mmm-yyyy-format-in-javascript[^]



C#
function customParse(str) {
     var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
             'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
   n = months.length, re = /(\d{2})-([a-z]{3})-(\d{4})/i, matches;

     while (n--) { months[months[n]] = n; } // map month names to their index :)

     matches = str.match(re); // extract date parts from string

     return new Date(matches[3], months[matches[2]], matches[1]);
 }

 function dtdatecompare()
 {
 alert(document.getElementById('ctl00_SampleContent_Date1').value);
 var dt =document.getElementById('ctl00_SampleContent_Date1').value;
 var currentTime = new Date();
 currentTime = currentTime.format('dd-MMM-yyyy');

/* var month = new Array();
 month[0] = "Jan";
 month[1] = "Feb";
 month[2] = "Mar";
 month[3] = "Apr";
 month[4] = "May";
 month[5] = "Jun";
 month[6] = "Jul";
 month[7] = "Aug";
 month[8] = "Sep";
 month[9] = "Oct";
 month[10] = "Novr";
 month[11] = "Dec";
 */
/// var cur = currentTime.split("-");
 alert(currentTime.toString());
 alert(customParse(dt));
 alert(customParse(currentTime));

 if (customParse(dt) > customParse(currentTime)) {
     alert('invalid');
 }
 else {
     alert('correct');
 }

 }
 
Share this answer
 
Comments
Anil Vijay Singh 22-Jan-14 0:50am    
right ans..thanks
use Convert.ToDateTime(string) : =>
C#
DateTime time = Convert.ToDateTime("10-10-2012");
            if (DateTime.Now <= time)
            {
                Response.Write("its work");
            }
 
Share this answer
 
Try This Example
C#
DateTime dtVal= Convert.ToDateTime("10-Jan-2012");
            if (DateTime.Now <= dtVal)
            {
                Response.Write("Big");
            }
            else
            {
                Response.Write("Small");
            }
 
Share this answer
 
C#
string date1 = System.DateTime.Now.ToString("dd-MMM-yyyy");
DateTime dt2 = Convert.ToDateTime(TextBox1.text);

DateTime dt1 = Convert.ToDateTime(date1);
        
        if (dt1 > dt2) Response.Write("bigDate"); 

use this code
 
Share this answer
 

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