Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript
How to compare text box date with current date which is in dd-MMM-yyyy format.
Posted 3-Jun-12 8:00am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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 at 3-Jun-12 13:34pm
   
this is much better than my solution, very good!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
Comments
Anil Vijay Singh at 22-Jan-14 0:50am
   
right ans..thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

use Convert.ToDateTime(string) : =>
DateTime time = Convert.ToDateTime("10-10-2012");
            if (DateTime.Now <= time)
            {
                Response.Write("its work");
            }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Try This Example
DateTime dtVal= Convert.ToDateTime("10-Jan-2012");
            if (DateTime.Now <= dtVal)
            {
                Response.Write("Big");
            }
            else
            {
                Response.Write("Small");
            }
 
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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)



Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 4 Jun 2012
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