1
votes

I have an operation that we run on a monthly basis, with an as_of_dt as the last calendar day of a month.

I have a working date selector on my dashboard, but I currently have a default date hardcoded. I'd like to make it default to the last calendar day of the previous month. So, for the month of june 2017, I want it to default to 5-31-2017.

Here is my current html:

<div class="form-group">
      <label class="mr-sm-2" for="date_input">Date</label>
      <input class="form-control" type="date" value="2017-05-31" id="date_input" name='date_input'>
    </div>

Thank you!

1
Once the page has been loaded you can dynamically calculate the last day of the month and assign it to your input element. I'll add an answer shortly. - Govind Rai

1 Answers

0
votes

You can dynamically calculate the last day of the month and assign it to your input element.

<input type="date" id="date">
<script>
  function getLastDayDate() {
    var today = new Date();
    var lastDayDate = new Date(today.getFullYear(), today.getMonth(), 0);
    var year = lastDayDate.getFullYear();
    var month = lastDayDate.getMonth().length == 2 ? lastDayDate.getMonth() + 1 : '0' + (lastDayDate.getMonth() + 1);
    var date = lastDayDate.getDate();
    document.getElementById("date").value = [year, month, date].join('-');
  }
  getLastDayDate();
</script>