1

I currently have this code:

      <div class="form-group row">
        <label for="example-date-input" class="col-2 col-form-label">Start Date</label>
        <div class="col-4">
          <input class="form-control" type="date" value="" id="start_date">
        </div>
        <label for="example-date-input" class="col-2 col-form-label">End Date</label>
        <div class="col-4">
          <input class="form-control" type="date" value="" id="end_date">
        </div>
      </div>

and This javascript:

        var now = new Date();
        
        var month = now.getMonth() + 1;
        var year = now.getFullYear();
        var day = now.getDate();
        
        var start_date = month + "/1/" + year;
        var end_date = month + "/" + day + "/" + year;
        
        $("#start_date").val(start_date);

But for some reason the value of #start_date on page load does not change. It remains the place holder "mm/dd/yyyy"

Can anyone help explain why this will not work?

FYI: I am using Bootstrap 4.5.3 and Jquery 3.5.1.

You can see a JSFIDDLE of my attempt, with an excerpt of some of my code here: https://jsfiddle.net/69tn0q4f/1/

Adam John
  • 23
  • 6
  • Wrap your JS code in a `$(document).ready(function(){...your code here ...})` to ensure your DOM is fully loaded and the elements you try to manipulate are actually available! – Lapskaus Oct 30 '20 at 13:39
  • @Lapskaus The method I'm using is shorthand for document ready. https://learn.jquery.com/using-jquery-core/document-ready/ – Adam John Oct 30 '20 at 13:43
  • maybe you should include that in the code you are providing us here then ? – Lapskaus Oct 30 '20 at 13:55

4 Answers4

0

Updated: quoting the answer from https://stackoverflow.com/a/12347050/1844843

Date control in HTML 5 accepts in the format of Year - month - day as we use in SQL If the month is 9, it needs to be set as 09 not 9 simply. So it applies for day field also.

So the following alteration will work.

var now = new Date();

var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);

var today = now.getFullYear()+"-"+(month)+"-"+(day) ;

$('#start_date').val(today);
0

You can try changing the text using

$('#start_date').value(start_date)

It's necessary change the date format to yyyy-MM-dd

0

Try this:

$(document).ready(function () {
    var now = new Date();

    var month = now.getMonth() + 1;
    var year = now.getFullYear();
    var day = now.getDate();

    $("#end_date").val(year + '-' + month + '-' + day);
    $("#start_date").val(year + '-' + month + '-' + '01');
})

$("#end_date").val(year + '-' + month + '-' + day); in this line, the value of day has to be 2 digit number.

Wahid Masud
  • 993
  • 10
  • 35
0

I hope I understood your question correctly.

var now = new Date();
   
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var year = now.getFullYear();
var day = ("0" + now.getDate()).slice(-2);
   
var start_date = (year) + "-" + (month) + "-" + "01";  
var end_date = (year) + "-" + (month) + "-" + (day);
   
$('#start_date').val(start_date);       
$('#end_date').val(end_date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group row">
        <label for="example-date-input" class="col-2 col-form-label">Start Date</label>
        <div class="col-4">
          <input class="form-control" type="date" id="start_date">
        </div>
        <label for="example-date-input" class="col-2 col-form-label">End Date</label>
        <div class="col-4">
          <input type="date" id="end_date">
        </div>
</div>
s.kuznetsov
  • 14,870
  • 3
  • 10
  • 25