Solved

Compare 2 dates in dd/mm/yyyy format

Posted on 2004-04-06
4
24,108 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
Comment Utility
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
Comment Utility
@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
Comment Utility
@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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now