javscript to compare 2 dates
-
Hi guys, any help with a script to compare 2 dates in a web form!! I tried compare validator in VS2003, but it is not functioning well when using 2 dates in 2 different years.. Thanks in advance... SH
You can modify the following code to suit your need. //Ldt_due can be in mm/dd/yyyy or mm/dd/yyyy hh:mm:ss format
var l_dt = new Date(); var today = Date.UTC( l_dt.getFullYear(), l_dt.getMonth()+1, l_dt.getDate(),0,0,0); var Ldt_Year,Ldt_Month,Ldt_Day Ldt_Year = String(Ldt_due).substring(0,4) if ( String(Ldt_due).charAt(6) == "/" ){ Ldt_Month = String(Ldt_due).substring(5,6); if ( String(Ldt_due).charAt(8) == " " ){ Ldt_Day = String(Ldt_due).substring(7,8); } else{ Ldt_Day = String(Ldt_due).substring(7,9); } }//Char At6 else{ Ldt_Month = String(Ldt_due).substring(5,7); if ( String(Ldt_due).charAt(9) == " " ){ Ldt_Day = String(Ldt_due).substring(8,9); } else{ Ldt_Day = String(Ldt_due).substring(8,10); } }//Else starttime = Date.UTC( Ldt_Year, Ldt_Month , Ldt_Day,0,0,0); if ( starttime <= today ){ alert("Date Due cannot be less than or equal to today's date."); return 1; }
Regards, Satips.
-
Hi guys, any help with a script to compare 2 dates in a web form!! I tried compare validator in VS2003, but it is not functioning well when using 2 dates in 2 different years.. Thanks in advance... SH
We used to have a similar problem with the compare validator and two dates. We were entering dates in British dd/mm/yyyy format but the validators work by default with American mm/dd/yyyy dates. The answer is to include a globalization element in your web.config:
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB"/>
</system.web>Cheers, Dave.
-
We used to have a similar problem with the compare validator and two dates. We were entering dates in British dd/mm/yyyy format but the validators work by default with American mm/dd/yyyy dates. The answer is to include a globalization element in your web.config:
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB"/>
</system.web>Cheers, Dave.
U r right iam using this script with 'mm/dd/yyyy' format when iam filling the two dates: onclick="javascript:popUpCalendar(this, TxtFrom, 'mm/dd/yyyy',-10,-200)" i added the element u gave me but didnt change still giving alert i f i choosed a fromdate in 2006 and todate in 2007. Any ideas i would be thankful..
-
U r right iam using this script with 'mm/dd/yyyy' format when iam filling the two dates: onclick="javascript:popUpCalendar(this, TxtFrom, 'mm/dd/yyyy',-10,-200)" i added the element u gave me but didnt change still giving alert i f i choosed a fromdate in 2006 and todate in 2007. Any ideas i would be thankful..
I have never used a popup calendar because I think it is quicker for a user who may have just entered a name and address, perhaps, to continue to use their keyboard to enter a date (or two). Anyway, that should not be relevant to the immediate problem. Try the following... Create a page with the following content:
<%@ Page Language="VB" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Date Range:
<asp:Textbox id="txtDate1" runat="server" columns="15" Text="01/04/2006" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtDate1" ErrorMessage="Date must be entered" Display="Dynamic" />
to
<asp:Textbox id="txtDate2" runat="server" columns="15" Text="03/01/2009" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtDate2" ErrorMessage="Date must be entered" Display="Dynamic" />
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="Date must be in the format dd/mm/yyyy and it must be after the first date"
ControlToValidate="txtDate2" Type="Date" Display="Dynamic" Operator="GreaterThanEqual"
ControlToCompare="txtDate1" />
</div>
</form>
</body>
</html>... then test it, change the second date to 03/01/2006 and press the tab key. A suitable error message should appear. Change the second date to 03/01/2007, press the tab key and the error message should disappear. Hope that helps. If not try looking in C:\Inetpub\wwwroot\aspnet_client\system_web\1_1_4322\WebUIValidation.js (assuming you are running IIS and your website root folder is C:\Inetpub\wwwroot). I'm not sure if this is the up-to-date JavaScript validation code, especially if you are using ASP.NET 2.0 (or 3.0!) but the version number of 125 suggests it is OK. I really hope my example helps because the JavaScript code takes a fair bit of time to get to understand. Cheers, Dave. :) -- modified at 15:29 Tuesday 2nd January, 2007