2

I've got a startdate and enddate from inputs. And I need to put all the dates from the startdate until the enddate into the database. Therefore I need to make a loop like this:

FOR i = startdate; i <= enddate; i + 1 day 
{
   here i use the date
}

How do I make such a loop with dates from input boxes?

I get 'invalid date' if I try to do this:

var endDate = new Date($("#enddate").val());

And I can't use the endDate.getTime() like I need as you said in the answer, if I do it like this.

var endDate = $("#enddate").val());
var endDateTime = endDate.getTime();

So basically: How can I convert the input to a date? The input of enddate is like this: dd/mm/yyyy.

No it's not an SQL question, I need to do this is javascript because I need to check the dates first.

Thank you for helping me out ;)

3
  • 1
    Any change you can do this in SQL instead of a javascript loop? It will most likely be faster. Assuming by your question I understand correctly that you are inserting multiple records based solely on the dates. Commented Feb 12, 2013 at 20:37
  • What Database are you using? Commented Feb 12, 2013 at 20:39
  • Is this a JavaScript or a SQL question? Commented Feb 12, 2013 at 20:44

1 Answer 1

2

Would a loop like this work?:

var current_date = new Date("01/13/2013");
var end_date = new Date("01/20/2013");
var end_date_time = end_date.getTime();

while (current_date.getTime() < end_date_time) {
    console.log(current_date);
    current_date.setDate(current_date.getDate()+1);
}

http://jsfiddle.net/Sn6Ws/

Depending on the format of your textboxes' values, you can set it up like this:

$(document).ready(function () {
    $("#btn").on("click", function () {
        dateLooper(function (cur, end) {
            console.log("Current date: " + cur.toString() + ", End Date: " + end.toString());
        });
    });
});

function dateLooper(callback) {
    var start_date_text = document.getElementById("start_date").value;
    var end_date_text = document.getElementById("end_date").value;

    var current_date = new Date(start_date_text);
    var end_date = new Date(end_date_text);
    var end_date_time = end_date.getTime();

    while (current_date.getTime() < end_date_time) {
        //console.log(current_date);
        callback.call(this, current_date, end_date);
        current_date.setDate(current_date.getDate()+1);
    }
}

http://jsfiddle.net/Sn6Ws/1/

Per your comments that explain the date are in the format "dd/mm/yyyy", you could use something like this:

var start_date_text = document.getElementById("start_date").value;
var start_split = start_date_text.split("/");
if (start_split.length != 3) {
    return false;
}
start_date_text = start_split[1] + "/" + start_split[0] + "/" + start_split[2];

var end_date_text = document.getElementById("end_date").value;
var end_split = end_date_text.split("/");
if (end_split.length != 3) {
    return false;
}
end_date_text = end_split[1] + "/" + end_split[0] + "/" + end_split[2];

to get the dates in the right format before passing them to new Date. Here's an updated jsFiddle that demonstrates it:

http://jsfiddle.net/Sn6Ws/4/

Of course, be careful that if the dates don't come in with the specified format (in case users can type this in or something), the code will most likely throw an error. You can obviously put more checks in to make sure certain things set before proceeding with certain operations (like making sure each item is a number/integer, making sure the days are in the range 1 to 31, etc.). So for that reason, you may want to go the route of regular expressions. At least with regular expressions, you can specify a specific pattern and know whether it matches perfectly or not, and immediately get the values you need to build a date.

Using regular expressions, here's an example that isn't complete but should hopefully help:

function dateLooper(callback) {
    var re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;

    var start_date_text = document.getElementById("start_date").value;
    var start_match = re.exec(start_date_text);
    if (start_match) {
        var valid = true;
        // Set `valid` variable based on the following
        // Validate start_match[1] is valid day
        // Validate start_match[2] is valid month
        // Validate start_match[3] is valid year
        if (valid) {
            start_date_text = combineDate(start_match);
        } else {
            return false;
        }
    } else {
        return false;
    }

    var end_date_text = document.getElementById("end_date").value;
    var end_match = re.exec(end_date_text);
    if (end_match) {
        var valid = true;
        // Set `valid` variable based on the following
        // Validate end_match[1] is valid day
        // Validate end_match[2] is valid month
        // Validate end_match[3] is valid year
        if (valid) {
            end_date_text = combineDate(end_match);
        } else {
            return false;
        }
    } else {
        return false;
    }

    var current_date = new Date(start_date_text);
    var end_date = new Date(end_date_text);
    var end_date_time = end_date.getTime();
    var days_spent = 0;

    while (current_date.getTime() < end_date_time) {
        days_spent++;
        callback.call(this, current_date, end_date, days_spent);
        current_date.setDate(current_date.getDate()+1);
    }

    return days_spent;
}

function combineDate(re_match) {
    return re_match[2] + "/" + re_match[1] + "/" + re_match[3];
}

http://jsfiddle.net/Sn6Ws/6/

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

3 Comments

I can totally use that while-loop, but it doesn't work because I can't convert the value of the input to a Date format. Could you edit your code if you know the answer?
@user2066100 I updated my answer with some explanation. Try out the last example and let me know if that helps
The split helped me out to make it a date format. Already had a validation. But thanks.

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.