Solved

Compare 2 dates in dd/mm/yyyy format

Posted on 2004-04-06
4
24,109 Views
Last Modified: 2011-08-18
I need to compare the date input by a user with the current date. If the userdate is before the current date, display error message, if not submit form. I have this function which works fine, but uses the windows US date format even though I changed the regional settings to use the dd/mm/yyyy format. According to Microsoft help, I need to get something called "Windows Multilingual User Interface Pack", which I cant seem to download anywhere. Besides, that seems to work individually for each browser terminal, and I need a standard format for all users, nomatter what browser language and format they are using.

Here is my actual function:

function chkdate()
{
currdate = new Date()
userdate = new Date(document.abrircaja.fecha_cierre.value)

currdate.setDate(currdate.getDate()-1)

var result = true;

      if (currdate <= userdate){
            
                                if (!confirm("Esta seguro que quiere abrir caja para el '" +  new Date document.abrircaja.fecha_cierre.value) + "' ?")){
            document.abrircaja.fecha_cierre.focus();
            return false;      
            }
            result = true;

            }else{

            alert(currdate);
            alert('No puede abrir caja con Fecha Cerrada.')
            document.abrircaja.fecha_cierre.focus();
            result = false;
      }
return result;
}
0
Comment
Question by:The-Jimmy
  • 3
4 Comments
 
LVL 19

Expert Comment

by:Dexstar
ID: 10767189
Use this function:
      function IsFutureDate( str1 )
      {
            // Parse the string in DD/MM/YYYY format
            re = /(\d{1,2})\/(\d{1,2})\/(\d{4})/
            var arr = re.exec( str1 );
            var dt1 = new Date( parseInt(arr[3]), parseInt(arr[2], 10) - 1, parseInt(arr[1], 10) );
      
            return dt1 > new Date();
      }

It will test to see if the str parameter is a date in the future or not.  If it does, it returns true.

Hope That Helps,
Dex*
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 10767211
@The-Jimmy:

After you add that function, change this line:
    if (currdate <= userdate){

To this:
    if (! IsFutureDate(document.abrircaja.fecha_cierre.value)) {

Dex*
0
 
LVL 19

Accepted Solution

by:
Dexstar earned 125 total points
ID: 10767368
@The-Jimmy:

Reading your code more closely, I think this is a better way to go:

    function ParseDate( str1 )
    {
        // Parse the string in DD/MM/YYYY format
        re = /(\d{1,2})\/(\d{1,2})\/(\d{4})/
        var arr = re.exec( str1 );
        return new Date( parseInt(arr[3]), parseInt(arr[2], 10) - 1, parseInt(arr[1], 10) );
    }

    function chkdate()
    {
        var userdate = ParseDate(document.abrircaja.fecha_cierre.value);
        var currdate = new Date();
        var result = false;

        currDate.setDate( currDate.getDate() - 1 );
        if ( userDate > currDate )
        {
            if (confirm("Esta seguro que quiere abrir caja para el '" +  userdate + "' ?"))
            {
                result = true;
            }
            else
            {
                document.abrircaja.fecha_cierre.focus();
            }
        }
        else
        {
            alert('No puede abrir caja con Fecha Cerrada:' + userdate );
            document.abrircaja.fecha_cierre.focus();
        }
        return result;
    }

Lemme know how that works out for you.

Dex*
0
 
LVL 2

Author Comment

by:The-Jimmy
ID: 10767493
Thanks ... they both work fine. In the second though, had to change currDate to currdate, since JS is case sensitive.

Thanks!

The-Jimmy
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now