Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. javscript to compare 2 dates

javscript to compare 2 dates

Scheduled Pinned Locked Moved ASP.NET
toolshelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samerh
    wrote on last edited by
    #1

    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

    S D 2 Replies Last reply
    0
    • S samerh

      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

      S Offline
      S Offline
      Sathesh Sakthivel
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S samerh

        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

        D Offline
        D Offline
        DaveRRR
        wrote on last edited by
        #3

        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.

        S 1 Reply Last reply
        0
        • D DaveRRR

          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.

          S Offline
          S Offline
          samerh
          wrote on last edited by
          #4

          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..

          D 1 Reply Last reply
          0
          • S samerh

            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..

            D Offline
            D Offline
            DaveRRR
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups