Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9274599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:23:29+00:00 2026-06-18T16:23:29+00:00

I’ve got a startdate and enddate from inputs. And I need to put all

  • 0

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 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:23:30+00:00Added an answer on June 18, 2026 at 4:23 pm

    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a text area in my form which accepts all possible characters from
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.