0

I try to develop functions on javascript to allow me, thanks to buttons, to also feed my input type Date.

Entre le <input type="date" id="debut" name="dateDeb" {% if debut is defined %} value={{debut}} {% endif %}/>
et le <input type="date" id="fin" name="dateFin" {% if fin is defined %} value={{fin}} {% endif %}/>

<input type="text" id="debut"/>
<input class="btn btn-primary" type="button" id="aujourdhui" value="Aujourd'hui"/>
<input class="btn btn-primary" type="button" id="semaine" value="Cette semaine"/>
<input class="btn btn-primary" type="button" id="mois" value="Ce mois-ci"/>

For example, at the click of "Today", I want my input date to take the value of the day. So I have this:

// function to update dates at today
$('#aujourdhui').click(function() {
    var today = new Date();
    var jj = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
    var aaaa = today.getFullYear();

    today = jj + '/' + mm + '/' + aaaa;

    alert(today);
    document.getElementById("debut").value = today;
});

the "alert" returns the date of the day, but my input remains unchanged. Can someone point me? Thank you !

3 Answers 3

1

The date need to be in the format yyyy-MM-dd. Use the toISOString function:

var today = new Date();
var todayStr = today.toISOString().slice(0,10);

document.getElementById("debut").value = todayStr;
Sign up to request clarification or add additional context in comments.

5 Comments

You might want to mention ISO 8601.
Oh ok I did not know this way! It works thank you! If I want this time to get two different dates (one with the date of the beginning of week and the other with the weekend), it is possible also with your method? Same for the month?
@eronn For a different question than the initial one, please ask a new question. I suggest you explore this in the console of your browser's developer tools: new Date().toISOString();.
Okay, I will wait 90 min for new question, thanks ^^
@eronn If you do as suggested in my previous comment, that question will probably not be necessary.
0

The real problem is that you have 2 input with the same id debut

1 Comment

Just rename the last text input and the event listener. Your code is right
0

You have to do correct date format 'YYYY-mm-dd':

 today = aaaa + '-' + mm + '-' + jj;

7 Comments

Sma problem, I already test this method, but it does nothing :/
please update input field: <input type="text" name ="debut" id="debut" value="">
We agree that I want the date to appear on my input date? The last one was just a test. By the way, I also tested for this one, and the same, no change
I can see there is two input fields having same name id "debut".Is it correct? If Yes, Id must be have unique.
Try to avoid jQuery when you can do it with vanilla JS
|

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.