Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
function validate_date(source, args) {
            var joinDate = document.getElementById('<%=txt_dateofjoin.ClientID %>').value;
            var confDate = document.getElementById('<%=txt_confirmationdate.ClientID %>').value;

            var dt = new Date(joinDate);
            var dt2 = new Date(confDate);
            var myDate = dt.format("dd-mm-yyyy");
            var myDate2 = dt2.format("dd-mm-yyyy");
            if (myDate < myDate2)
                args.IsValid = true;
            else
                args.IsValid = false;
        }

this code is not working. There are problem with "dt.format() function" I know it but how can I validate two dates now.
Posted
Updated 19-Aug-12 23:08pm
v2
Comments
AmitGajjar 20-Aug-12 4:21am    
are you getting any error ? or what is your problem ?
bbirajdar 20-Aug-12 4:56am    
You have written this code and asking us what's wrong with it ??.. I think there's nothing wrong with the code, but with the person who has written it..

With the very less information you have posted , I am posting my most probable guess

JavaScript
if (myDate < myDate2)
                args.IsValid = true;
            else
                args.IsValid = false;


Date of join should be less than the confirmation date. Only in that case the .IsValid is true

NOTE: Instead of using javascript, you can use the CompareValidator in asp.net
 
Share this answer
 
v2
Comments
Dinesh Ambaliya 20-Aug-12 5:09am    
I tried CompareValidator but it doesn't work with "dd-mm-yyyy" format.
bbirajdar 20-Aug-12 5:18am    
Did not work ? Thats impossible... You were not able to implement for this date format..

Give each a CompareValidator with Operator=DataTypeCheck and Type=Date to determine illegal dates

Add another CompareValidator to compare the two. Use ControlToValidate for startdate, ControlToCompare for enddate, Operator=LessThanEqual, and Type=Date

Now for the format of "dd/MM/yyyy". The validators use the current CultureInfo object of the thread to determine the date format. (CultureInfo.DateTimeInfo.ShortDatePattern specifically).

Set up the page to use the desired culture with <%@ Page culture="your culture" %>. Go to the .net docs topic for the CultureInfo class overview to see a list of cultures.
bbirajdar 20-Aug-12 5:19am    
Was your javascript problem solved ?
Alternative way to fullfill your rquirement is Use CompareValidator in following way...Make appropriate changes in value of properties in given tag

XML
<asp:CompareValidator  ID="CompareValidator1" runat="server"
 ErrorMessage="Confirm Date must be greater than Joining Date." ControlToCompare="TextBoxConfirmDate"
 ControlToValidate="TextBoxConfirmDate" ValueToCompare="TextBoxJoinDate"
 Operator="GreaterThan" Type="Date" ValidationGroup="GroupName"></asp:CompareValidator>
 
Share this answer
 
Why do you want to format and compare? Both are dates. So you no need to format before doing compare

try this.
C#
function validate_date(source, args) {
if(Date.Parse(document.getElementById('<%=txt_dateofjoin.ClientID %>').value<Date.Parse(document.getElementById('<%=txt_confirmationdate.ClientID %>').value)
args.IsValid=true;
else
args.IsValid=false;
 
Share this answer
 
v3
Comments
Dinesh Ambaliya 20-Aug-12 9:37am    
It's worked
Try this
C#
var dt = new Date(joinDate);
            var dt2 = new Date(confDate);
            var myDate = dt.getDate()
            var myDate2 = dt2.getDate();
            if (myDate < myDate2)
                args.IsValid = true;
            else
                args.IsValid = false;

Refer this link
http://srikanthtechnologies.com/blog/datecompare.html[^]
 
Share this answer
 
v2
 
Share this answer
 
Comments
Datona SRBG 29-Aug-21 14:06pm    
the start date must be greater than date of birth meaning

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