0

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

Dairo
  • 822
  • 1
  • 9
  • 22
Harish Gupta
  • 364
  • 2
  • 8
  • 23

2 Answers2

0

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]

CubanAzcuy
  • 125
  • 1
  • 13
  • it correct only i have just add the first colon just for the describing.In actual the stime is 00:00 – Harish Gupta Feb 07 '14 at 05:01
  • 1
    @user2841408 How is sdate and stime generated? the rest of the logic seems to work? Can you add more code or print out the results? – CubanAzcuy Feb 07 '14 at 05:03
  • its working in chrome but i am using ie8 and its not working in it – Harish Gupta Feb 07 '14 at 05:23
  • @user2841408 http://stackoverflow.com/questions/2182246/javascript-dates-in-ie-nan-firefox-chrome-ok http://stackoverflow.com/questions/2182246/javascript-dates-in-ie-nan-firefox-chrome-ok IE does dates different you need to write an if IE Method and do a date just for IE. – CubanAzcuy Feb 07 '14 at 05:55
  • @user2841408 I update my answer to slow the problem you were encountering it was browser specific Datetime formatting. – CubanAzcuy Feb 07 '14 at 06:17
  • Yeah, browser sniffing!! The new paradigm for 2014!! – RobG Feb 07 '14 at 06:27
  • @RobG sarcasm from another RobG I'm a Robert Gross. I'd probably think it was funnier if IE wasn't always the problem :-/. – CubanAzcuy Feb 07 '14 at 06:29
  • @CubanAzcuy—there is no reason to do browser sniffing here. IE isn't "always the problem", parsing of date strings was entirely implementation dependent before ES5, so any older browser (and there are lots of those other than IE 8 and lower) will not correctly parse an ISO 8601 string. The string in the OP will be treated as UTC, whereas the string generated via `new Date()` will be local time, so far better to have consistent methods for everyone, not just the lucky IE users. ;-) – RobG Feb 07 '14 at 06:50
0

That is a bit convoluted, consider:

var newdate = new Date();
var date_str = moment(newdate).format("DD-MMM-YYYY HH:mm");
Date.parse(date_str);

if the above works (and there is absolutely no guarantee that Date.parse will correctly parse the string in all browsers in use), then all of that is equivalent to:

var newdate = new Date();
newdate.setSeconds(0, 0);

You would do very much better to manualy parse haha (or use moment.js since you have it already) and compare the resultant date objects.

Consider:

// s is dd-mmm-yyyy hh:mm
function stringToDate(s) {
  s = s.split(/[- :]/);
  var months = {'jan':0, 'feb':1, 'mar':2, 'apr':3, 'may':4, 'jun': 5,
                'jul':6, 'aug':7, 'sep':8, 'oct':9, 'nov':10, 'dec':11};
  return new Date(s[2], months[s[1].toLowerCase()], s[0], s[3], s[4], 0, 0);
}

var newdate = '07-Feb-2014 10:04';
var haha    = '03-Feb-2014 00:00';

alert(stringToDate(newdate).getTime() == stringToDate(haha).getTime()); // false

// Set to same time
var newdate = '03-Feb-2014 00:00';

alert(stringToDate(newdate).getTime() == stringToDate(haha).getTime()); // true
RobG
  • 142,382
  • 31
  • 172
  • 209