Naveen J
Naveen J

Reputation: 13

how to avoid Bootstrap Datepicker default date choosen automatically?

I used Bootstrap DatePicker in a form, this is not a mandatory field. So whenever i submit a form without selecting the date on Datepicker. The default value like this 01-01-1970 only stored on datebase. I want to store like 0 instead of this value 01-01-1970.

I use Code like this

$('#dateRangePicker1').datepicker({
  format: 'dd/mm/yyyy',
  autoclose: true,
  minDate: 0
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script>

<div class="col-xs-12">
  <div class="form-group input-group input-append date" id='dateRangePicker4'>
    <input type="text" id="cust_dob" class="form-control" name="date4" value="" placeholder="DOB" />
    <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
  </div>
</div>


Upvotes: 0

Views: 1283

Answers (1)

Tree Nguyen
Tree Nguyen

Reputation: 1199

I do believe that by default, the bootstrap datepicker doesn't set the default value. You can check it here: Sandbox. You can open developer console and try

$('#sandbox-container input').datepicker('getDate')

This will return null for you. If this is not set to be required, null will be sent through form.

My belief is what you have set in your database is the cause of this behaviour. I think you have set the type to the column to be DATETIME, (not null, too) which makes the database to set in the default date, which is 01-01-1970 (the start of Unix timestamp). If this is the case, you can set the column type to be varchar, set the value to be some common format (YYYY-mm-dd HH:mm:ss is one of them). On client side, you can use some popular datetime library like MomentJS to parse the time or generate dateTime string to save to database.

Upvotes: 1

Related Questions