Solved

Compare 2 dates in dd/mm/yyyy format

Posted on 2004-04-06
4
24,124 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

717 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