1

I am looking for some help, trying to compare 2 dates in JavaScript, but haven't managed to find the exact answer on this site yet.

I am only just starting out with JavaScript and I get the impression that getting the current date\time is not a simple 1 line of code which I thought it would be. I thought I could use a ternary operator but maybe not. I hope I'm wrong but here is my dilemma.

I want to compare 2 dates. If date1 is before date2 then do something. If not then do something else.

  • date1: is the current date time. i.e. my system clock
  • date2: is a predefined value. I already have this value in the web page: ${expiredDateTime.format("ddMyyyyhm") I believe date 2 is a string but I'm no expert on jquery.

Any help with this would be great.

Eddie
  • 611
  • 3
  • 13
  • 23

4 Answers4

4

Please try the below code:

var user="12/11/2012/5/30";
var arrdt= user.split("/");
var userdt = new Date(arrdt[2], arrdt[1] - 1, arrdt[0],arrdt[3],arrdt[4]);
var currdt = new Date();
if (userdt < currdt) {
alert("userdate is before current date");
}else{
alert("userdate is after current date");
}
Sankar V
  • 4,110
  • 5
  • 28
  • 52
  • Sankar, I'm playing around with your code. Can you tell me what this line is doing? Date(arrdt[2], arrdt[1] - 1, arrdt[0],arrdt[3],arrdt[4]); – Eddie Feb 01 '13 at 13:15
  • That line will convert the user entered date (user="12/11/2012/5/30") to standard date format. To know more about it just: alert(userdt); – Sankar V Feb 01 '13 at 15:04
  • Thanks Sankar. I don't fully understand this array but it works and does what I was after, – Eddie Feb 01 '13 at 16:35
  • http://www.w3schools.com/js/js_obj_date.asp - I hope this link will help you to know more about JavaScript Date Object. – Sankar V Feb 01 '13 at 17:05
2

The easiest way to compare dates in javascript is to first convert it to a Date object and then compare these date-objects.

Below you find an object with three functions:

dates.compare(a,b)

Returns a number:

-1 if a < b 0 if a = b 1 if a > b NaN if a or b is an illegal date dates.inRange (d,start,end)

Returns a boolean or NaN:

true if d is between the start and end (inclusive) false if d is before start or after end. NaN if one or more of the dates are illegal. dates.convert

Used by the other functions to convert their input to a date object. The input can be

a date-object : The input is returned as is. an array: Interpreted as [year,month,day]. NOTE month is 0-11. a number : Interpreted as number of milliseconds since 1 Jan 1970 (a timestamp) a string : Several different formats is supported, like "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc. an object: Interpreted as an object with year, month and date attributes. NOTE month is 0-11.

 // Source: http://stackoverflow.com/questions/497790
    var dates = {
        convert:function(d) {
            // Converts the date in d to a date-object. The input can be:
            //   a date object: returned without modification
            //  an array      : Interpreted as [year,month,day]. NOTE: month is 0-11.
            //   a number     : Interpreted as number of milliseconds
            //                  since 1 Jan 1970 (a timestamp) 
            //   a string     : Any format supported by the javascript engine, like
            //                  "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
            //  an object     : Interpreted as an object with year, month and date
            //                  attributes.  **NOTE** month is 0-11.
            return (
                d.constructor === Date ? d :
                d.constructor === Array ? new Date(d[0],d[1],d[2]) :
                d.constructor === Number ? new Date(d) :
                d.constructor === String ? new Date(d) :
                typeof d === "object" ? new Date(d.year,d.month,d.date) :
                NaN
            );
        },
        compare:function(a,b) {
            // Compare two dates (could be of any type supported by the convert
            // function above) and returns:
            //  -1 : if a < b
            //   0 : if a = b
            //   1 : if a > b
            // NaN : if a or b is an illegal date
            // NOTE: The code inside isFinite does an assignment (=).
            return (
                isFinite(a=this.convert(a).valueOf()) &&
                isFinite(b=this.convert(b).valueOf()) ?
                (a>b)-(a<b) :
                NaN
            );
        },
        inRange:function(d,start,end) {
            // Checks if date in d is between dates in start and end.
            // Returns a boolean or NaN:
            //    true  : if d is between start and end (inclusive)
            //    false : if d is before start or after end
            //    NaN   : if one or more of the dates is illegal.
            // NOTE: The code inside isFinite does an assignment (=).
           return (
                isFinite(d=this.convert(d).valueOf()) &&
                isFinite(start=this.convert(start).valueOf()) &&
                isFinite(end=this.convert(end).valueOf()) ?
                start <= d && d <= end :
                NaN
            );
        }
    }
Engineer
  • 5,911
  • 4
  • 31
  • 58
  • @Eddie once you will convert those date into js date objects, you can compare normal > and < operators, and to get current date just use new Date(); – Rahul R. Feb 01 '13 at 12:05
0

Possible douplicate for link

if (date1.getTime() > date2.getTime()) {
    alert("The first date is after the second date!");
}

Reference to Date object

Community
  • 1
  • 1
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
0

try below code for getting current month, year , date

var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
Ripa Saha
  • 2,532
  • 6
  • 27
  • 51