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
Solved

Compare 2 dates in dd/mm/yyyy format

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Grunt script for Build Process 1 42
Alert after MVC form submitted 1 22
multiple selects 23 48
javascript - insert into js doc help 2 22
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

839 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