problem with bootstrap datepicker in MVC app

problem with bootstrap datepicker in MVC app

I'm somewhat new to jquery and trying to get familiar with coding it.  In my mvc app there is a bootstrap datepicker (not datetimepicker which i couldnt make work)

The problem and challenge is this,

there are about 50 date fields.  They are cast as string within MVC.  In some records some fields will have a date already there, most will be blank.  If there is already a date, it could be any format, if blank the formatting should be 07/04/1776 and actually if should always get converted down to that 07/04/1776 style, which i think is mm/dd/yyyy ?

if you click in the textbox, i made a function to convert whats in the textbox into this format, 

  1. $('#Astr').on('click', function (e) {
  2.                             alert("clicked");
  3.                             var st = $('#Astr').val();
  4.                             var dt = new Date(st);
  5.                             var day = dt.getDate();
  6.                             // alert(day);
  7.                             var year = dt.getFullYear();
  8.                             var mi = dt.getMonth() + 1;
  9.                             var mytxt = mi + '/' + day + '/' + year;
  10.                             $('#Astr').val(mytxt);
  11.                         })

Trouble comes when i tried to add a button to the textbox area to call the datepicker.  It calls it but does not go away when an item is clicked.  it does not have a clear nor close button

  1.  <script type="text/javascript">
  2.         function setCalendar()
  3.         {
  4.             alert("///");
  5.             //var date1;
  6.             //$('#date1').datepicker('show');

  7.             $('#Astr').datepicker('show',{
  8.                 format: "MM d, yyyy",
  9.                 clearBtn: true,
  10.                 autoclose: true
  11.             }).on('changeDate', function (e) {
  12.                 $.datepicker('hide')
  13.                });

  14.             $.fn.datepicker.dates['en'] = {
  15.                 days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
  16.                 daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  17.                 daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
  18.                 months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  19.                 monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
  20.             };
  21.         }
  22.     </script>

And the textbox itself that is in the mvc view,
  1.  <div class="form-group">
  2.                 @Html.LabelFor(model => model.Astr, new { @class = "control-label col-md-3" })
  3.                 <div class="col-md-9">
  4.                     @Html.EditorFor(model => model.Astr, new { @class = "CreatedOn" })
  5.                     @Html.ValidationMessageFor(model => model.Astr)
  7.                     <button type="button" class="btn-success" id="ddd" onclick="setCalendar()">
  9.                     </button>
  10.                 </div>
  11.             </div>
I set the format type of this datepicker to MM d, yyyy because this way it recognizes the month if its text eg. "July"  otherwise datepicker does not know July = 7 and always inserts the current month, September in this case.

Also, the datepicker tends to force its output onto the textbox irregardless, it needs to be blocked so that only if explicitly clicked will its output modify the textbox, which is some important date another user entered.

right now, or at least recently, if the datepicker appears? if you click outside, if the textbox had: July 4, 1776? it fails to read the month and inserts:  09/04/1776, which is what it shows, September 1776 with the day 4 highlighted.

any help or ideas are appreciated