Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NETJavascript
How to compare text box date with current date which is in dd-MMM-yyyy format.
Posted 3 Jun '12 - 7:00


5 solutions

Hi!
 
Try following code:
 
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.
  Permalink  
Comments
taha bahrami - 3 Jun '12 - 13:34
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[^]
 

 
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');
 }
 
 }
  Permalink  
use Convert.ToDateTime(string) : =>
DateTime time = Convert.ToDateTime("10-10-2012");
            if (DateTime.Now <= time)
            {
                Response.Write("its work");
            }
  Permalink  
Try This Example
DateTime dtVal= Convert.ToDateTime("10-Jan-2012");
            if (DateTime.Now <= dtVal)
            {
                Response.Write("Big");
            }
            else
            {
                Response.Write("Small");
            }
 
  Permalink  
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
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Mohammed Hameed 268
1 OriginalGriff 236
2 Sergey Alexandrovich Kryukov 153
3 Mayur_Panchal 153
4 CPallini 100
0 Sergey Alexandrovich Kryukov 8,171
1 OriginalGriff 6,246
2 CPallini 3,532
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 4 Jun 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid