0

I'm trying to set come dates as values by default when the bootstrap-datetimepicker gets initialised. The thing is that I need to set the current date as date_till.val() and the date_from.val() must be a 'two-week-ago' date. And I need to make it in the format of 'DD.MM.YYYY'. How would I do that?

I have something like this now but it sets only the current date:

var CurrentDate = new Date();

    $('.date_from').val(CurrentDate);
    $('.date_till').val(CurrentDate);
Purvik Dhorajiya
  • 4,662
  • 3
  • 34
  • 43
JDoeBloke
  • 551
  • 4
  • 7
  • 19

5 Answers5

0

change bootstrap date picker format

$(function() {              
           // Bootstrap DateTimePicker v4
           $('#datetimepicker').datetimepicker({
                 format: 'DD/MM/YYYY'
           });
        });   

And to provide default Dates in this plugin, use Moment.js , easy to use if its not what you want then inform me i will change my solution

Krunal Limbad
  • 1,533
  • 1
  • 12
  • 23
0

12096e5 is a magic number which is 14 days in milliseconds.

$(document).ready(function() {
  var date = new Date();
  var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  var pastdate = new Date(+new Date - 12096e5);


  $('#date_from').datepicker({
    format: "mm/dd/yyyy",
    todayHighlight: true,
    startDate: pastdate,
    autoclose: true
  });
  $('#date_till').datepicker({
    format: "mm/dd/yyyy",
    todayHighlight: true,
    startDate: today,
    autoclose: true
  });

  $('#date_from').datepicker('setDate', pastdate);
  $('#date_till').datepicker('setDate', today);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/js/bootstrap-datepicker.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/css/bootstrap-datepicker.min.css" />
<div class="row">
  <div class="col-lg-2">
<div class="panel">
  <fieldset>
    <div class="form-group">
      <label for="date_from">Date From</label>
      <input type="text" class="form-control" id="date_from">
    </div>
  </fieldset>
  <fieldset>
    <div class="form-group">
      <label for="date_till">Date Till</label>
      <input type="text" class="form-control" id="date_till">
    </div>
  </fieldset>
</div>
  </div>
</div>

Would you please check my above snippet?

Purvik Dhorajiya
  • 4,662
  • 3
  • 34
  • 43
0
var dateTillDate = new Date();
var fromDate = new Date()
fromDate.setDate(-14);

// till date

$(".date_till").datepicker({
    format: 'DD/MM/YYYY',
}).datepicker("setDate", dateTillDate); 

// from date

 $(".date_from").datepicker({
        format: 'DD/MM/YYYY',
   }).datepicker("setDate", fromDate);
Nayas Subramanian
  • 2,269
  • 21
  • 28
0

Hope this will help.

var today = new Date();
var date_till =  today.getDate()+"."+(today.getMonth()+1)+"."+today.getFullYear();
today.setDate(today.getDate() -14); /*Two week back date*/
var dd = today.getDate();
var mm = today.getMonth()+1; /*Because January is 0*/
var YY = today.getFullYear();
$(".date_from").val(dd+'.'+mm+"."+YY);
$(".date_till").val(date_till);
-1

you can subtract 14 days form current date

$('.date_from').val(CurrentDate.getDate() -14);

And for formatting. you have to manually concat using method date.getDate();date.getDate();date.getFullYear()

Update CurrentDate.setDate(CurrentDate.getDate()-14);

FosterZ
  • 3,863
  • 6
  • 38
  • 62