0

When I'm trying to forget datepicker's date strings, I keep getting a blank result. How do I properly format the value?

//first attempt
$("#calendar-container").datepicker( {
    maxViewMode: 1,
    todayHighlight: true,
    dateFormat: 'dd-mm-yy'
})
.on('changeDate', function() {
    var date = $("#calendar-container").datepicker('getDate');
    alert(date); //returns Tue Aug 06 2019 00:00:00 GMT-0400 (Eastern Daylight Time), no formatting
});

//second attempt
$("#calendar-container").datepicker( {
    maxViewMode: 1,
    todayHighlight: true
})
.on('changeDate', function() {
    var date = $("#calendar-container").datepicker({ dateFormat: 'dd-mm-yy' }).val();
    alert(date); //returns blank
});
Tony White
  • 197
  • 5
  • 18
  • instead of `datepicker('getDate')`, try `val()` – User863 Aug 14 '19 at 14:42
  • The new line reads `var date = $("#calendar-container").val();`, but is still returning an empty string. – Tony White Aug 14 '19 at 14:43
  • is `#calendar-container` an `input` field? – User863 Aug 14 '19 at 14:44
  • No, it's an embedded version of the datepicker api, in the HTML is this: `
    `
    – Tony White Aug 14 '19 at 14:45
  • Try with `.text()` i.e : `$("#calendar-container").datepicker({ 'dateFormat: dd-mm-yy' }).text();` – Swati Aug 14 '19 at 14:50
  • @Swati I get the text of the entire datepicker api, not the selected date. Also, I've corrected an error that I had in writing this post, I had `.datepicker({ 'dateFormat: dd-mm-yy' }).val();` but it should be `dateFormat: 'dd-mm-yy'` with single quotes only around the format, not the attribute. – Tony White Aug 14 '19 at 15:02

1 Answers1

1

You are searching for getFormattedDate

Note : dateFormat is not valid option. Use format instead

$('#calendar-container').datepicker('getFormattedDate')

$("#calendar-container").datepicker({
  maxViewMode: 1,
  format: 'dd-mm-yy',
  todayHighlight: true
}).on('changeDate', function() {
    var date = $("#calendar-container").datepicker('getFormattedDate');
    console.log(date);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.standalone.min.css" integrity="sha256-jO7D3fIsAq+jB8Xt3NI5vBf3k4tvtHwzp8ISLQG4UWU=" crossorigin="anonymous" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js" integrity="sha256-bqVeqGdJ7h/lYPq6xrPv/YGzMEb6dNxlfiTUHSgRCp8=" crossorigin="anonymous"></script>

<div id="calendar-container"></div>
User863
  • 19,346
  • 2
  • 17
  • 41
  • The issue with this is it returns `8/14/19`. I'm new to jquery, so I'm taking this one step at a time, this step being that I am using the `alert()` method to make sure it's properly formatted, but the next step is have jquery open a txt file, and compare the input string to the lookup table. The dates in the lookup table when we receive them are formatted with hyphens instead of slashes, and I'm trying to eliminate the step of having to replace slashes with hyphens in the txt file. I'm attempting to set up dateFormat to return it in like `08-14-19` to prevent having to edit the file – Tony White Aug 14 '19 at 14:56
  • I could have swore I had format option as you have in your edit, but apparently I overlooked that while attempted your solution. Thank you for the assistance! Question if you have the time, I see similar questions to mine, the answers to which I've tried and still ran into the problem I described above. https://stackoverflow.com/questions/1328025/jquery-ui-datepicker-change-date-format?rq=1 this for example. Do you know why this solution wasn't working for me? is it because `alert()` just can't handle `val()` strings? – Tony White Aug 14 '19 at 15:17
  • 1
    @TonyWhite I hope you know the `jquery-ui-datepicker` and `bootstrap-datepicker` are not same – User863 Aug 14 '19 at 15:19
  • I do now, tunnel vision, only saw "datepicker", saw the body of the thread had similar code to mine, didn't notice the title said jquery-ui-datepicker. Thanks against for the assistance! – Tony White Aug 14 '19 at 15:21