JavaScript How to construct date_select values into single date format using Javascript/JQuery?

I have a date_select in my html form. I know that it separates the day month and year. I am trying to work out how to construct a date in the form YYYY-MM-DD within my Javascript, in order to send this date to my controller to be compared against other dates.

Here is what I've got so far (I am a JQuery/JS beginner):

$(document).on('click', "#check-button", function(){

    var year = $("#event_day_1i").val();
    var month = $("#event_day_2i").val();
    var day = $("#event_day_3i").val();
    var selectedDate = [year + "-" + month + "-" + day];

...
$.ajax({
            url: '/events/check',
            data: {checkList: checkList , selected_date: selectedDate },
            method: "POST"
        }
    );
)};

But this creates dates such as "2016-2-4" instead of "2016-02-04". What is the best way of fixing this problem?

If you have a solution that allows me to send the 3 separate values to be constructed into a date within my controller then that is also great, but I don't know how I would do that either (beginner).

Thanks!

Answer:1

another way of prepending the "0" is to use the following (only a portion of the code listed):

day = ("0" + day).slice(-2);

month = ("0" + month).slice(-2);

what this does is add the "0" to the start and then takes the last two characters. Note that both this and the other way return these values back to strings.

Answer:2

As part of an image gallery I am using the below code to switch out the src attribute on an image element as a user clicks on various thumbnails. Every part of it has been working fine for about 7 ...

As part of an image gallery I am using the below code to switch out the src attribute on an image element as a user clicks on various thumbnails. Every part of it has been working fine for about 7 ...

  1. attribute change doesn't work

I have this string: abcabca and this regex: /abca/g this one only matchs the first abca but I want it to match the second one too.

I have this string: abcabca and this regex: /abca/g this one only matchs the first abca but I want it to match the second one too.

The following code hangs on Firefox and Chrome but not on Edge: let sequence = Promise.resolve(); function fire() { sequence.then(fire) }; fire(); I think it didn't hang on Firefox a few versions ...

The following code hangs on Firefox and Chrome but not on Edge: let sequence = Promise.resolve(); function fire() { sequence.then(fire) }; fire(); I think it didn't hang on Firefox a few versions ...

  1. promise then asynchronous

I'm trying to trigger a change in the content of a fixed div when you scroll past the top of an image. $(window).scroll(function() { var y_scroll_pos = window.pageYOffset; var img_position = ...

I'm trying to trigger a change in the content of a fixed div when you scroll past the top of an image. $(window).scroll(function() { var y_scroll_pos = window.pageYOffset; var img_position = ...