Solved

Compare 2 dates in dd/mm/yyyy format

Posted on 2004-04-06
4
24,111 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

770 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