Search code examples
javascriptdatemomentjsdate-comparison

comparing dates in JavaScript using moment with langs


I have two dates namely newdate and haha. newdate will be today's date (current date) and haha date can be any.The below code is not working for me as i have provided
newdate : 07-Feb-2014 10:04
haha :03-Feb-2014 00:00
its always coming to else part
sdate:03-Feb-2014
stime :00:00

var haha=sdate+" "+stime;
    var newdate=new Date();
                  var date_str = moment(newdate).format("DD-MMM-YYYY HH:mm");
                alert(date_str);
                  if (Date.parse(haha) < Date.parse(date_str)) {

                  alert("Start date cannot be less than today's date");

                  return false;

                  }
                  else {

                      alert("hahahhahaha");
                  }

NOTE I am using moment with langs javscript


Solution

  • Your Code Works. Stime is formatted wrong remove the colon from in front of the first set of 00. stime 00:00. How are you generating stime this is the cause of you problem?

    You can see my test here.

    var sdate = "03-Feb-2014";
    var stime = "00:00";
    var haha = sdate + " " + stime;
    var newdate = new Date();
    
    if (navigator.appName.indexOf("Internet Explorer") != -1) {
        alert("isIE");
        var dateObject = (parseISO8601(haha));
        var hahaDate = new Date(dateObject.year, dateObject.month, dateObject.day, dateObject.hour, dateObject.min);
        alert(hahaDate);
        if (hahaDate.getTime() < newdate.getTime()) {
            alert("Start date cannot be less than today's date");
            return false;
        } else {
            alert("hahahhahaha");
        }
    } else {
    
        var date_str = moment(newdate).format("DD-MMM-YYYY HH:mm");
        alert(date_str);
        if (Date.parse(haha) < Date.parse(date_str)) {
            alert("Start date cannot be less than today's date");
            return false;
        } else {
            alert("hahahhahaha");
        }
    }
    
    function parseISO8601(dateStringInRange) {
        var dateAsObject = {};
    
        var splitTimeFromDate = dateStringInRange.split(" ");
    
        var splitTimeValues = splitTimeFromDate[1].split(":");
    
        dateAsObject.hour = splitTimeValues[0];
        dateAsObject.min = splitTimeValues[1];
    
        var splitDate = splitTimeFromDate[0].split("-");
        dateAsObject.year = splitDate[2];
        dateAsObject.day = splitDate[0];
        dateAsObject.month = monthToNum(splitDate[1]);
        return dateAsObject;
    
    }
    
    function monthToNum(month) {
        if (month == "Feb") return 1;
    }
    

    [Edit: Ok sorry I messed up with the Colon, If it fails at the else are you sure you unit tests include enough scenario to were the date is both greater than and less than the current date if it is only less than like your example you will never hit the code in the else. Again the code just works don't know what to say :-(, update example for both situations]

    [Edit: Here is an example not complete you have to remember javascript is not universal. When you ask a question about JS assume as DEVs we all use Chrome or FF, or atleast post the browser(s) you tired. I provided a simple example of how I would accomplish this. Frankly I don't like external framework when I can do it myself so as you can see I am not using it feel free to do what you want the issue is cause by the way IE Parses DateTime you must use a more universal format like the one provided below. Example of possible formats: http://www.w3schools.com/jsref/jsref_obj_date.asp. Anyhow GL]