3
function mainFunc() {
    dueDate = "30/12/2014";
    var firstReminderDate = dueDate;
    var today = new Date();
    var firstDate = convertToDate(firstReminderDate);
    if (today > firstDate) {
        //send reminder to A
    } else {
        // send reminder to B
    }
}

function convertToDate(dateString) {
    var dateData = dateString.split("/");
    var date = new Date(new Date().setFullYear(dateData[0], dateData[1] - 1, dateData[2]));
    return new Date(date);
}

I need to compare two dates not the time, and how to remove the time part and just compare the dates? The convertToDate() is returning the "Thu Jan 01 05:30:00 GMT+05:30 1970" everytime?

3
  • 1
    use the ISO format: YYYY-MM-DD, new Date(dateData[2] + '-' + dateData[1] + '-' + dateData[0]) Commented Dec 17, 2014 at 8:35
  • The input I am getting as dd/mm/yyyy only Commented Dec 17, 2014 at 8:36
  • 1
    Date calculations/manipulations? -> Use a library such as moment.js :) There are so many pitfalls with dates (formats, leap years, daylight saving time, ...) Commented Dec 17, 2014 at 8:42

4 Answers 4

3

You can simplify your code. To get a date from dd/mm/yyyy, simply splitting on /, reversing the result and joining it on '/' gives you yyyy/mm/dd, which is valid input for a new Date to compare to some other Date. See snippet

var report = document.querySelector('#result');
report.innerHTML += '30/12/2014 => '+ mainFunc('30/12/2014');
report.innerHTML += '<br>01/12/2014 => '+ mainFunc('01/01/2014');

function mainFunc(due) {
    due = due ? convertToDate(due) : new Date;
    return new Date > due 
           ? due +' passed: <b>send reminder to A</b>'
           : due +' not passed: <b>send reminder to B</b>';
}

function convertToDate(dateString) {
    return new Date(dateString.split("/").reverse().join('/'));
}
<div id="result"></div>

Sign up to request clarification or add additional context in comments.

Comments

0

Just return it in milliseconds format

function convertToDate(dateString) {
    var dateData = dateString.split("/");
    return +new Date(new Date().setFullYear(dateData[0], dateData[1] - 1, dateData[2]));
}

And also change var today = new Date(); to var today = +new Date();. Now it should work. + here converts Date object to milliseconds.

Comments

0

The best way to compare two date is to instanciate them with same object, here you must use Date object.

function mainFunc(){
    var firstDate = new Date( dueDate = "30/12/2014" );
    today = new Date(); 

    if( today > firstDate ){
     //...
    }
    else{
     //...
    }
}

Comments

0

I will recomend momentjs lib to parse, validate, manipulate, and display dates in JavaScript.

var firstDate = moment("30/12/2014", "DD/MM/YYYY")
var today = moment();

// Format to Unix Timestamp to compare
if(today.format('X') > firstDate.format('X')){

  //send reminder to A
}else{

  // send reminder to B
}

Here is the link http://momentjs.com/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.