最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the changed date from bootstrap-datepicker - Stack Overflow

programmeradmin2浏览0评论

I am using Eternicode's fork of the bootstrap-datepicker library and until now all is going well for generating it, but I want to get the date in order to use it in my logic after changing the date.

$('#calendar').on('changeDate', function(event, date) {
  // how to pop alert message to print the date I've chosen ?
});

How can I do this?

I am using Eternicode's fork of the bootstrap-datepicker library and until now all is going well for generating it, but I want to get the date in order to use it in my logic after changing the date.

$('#calendar').on('changeDate', function(event, date) {
  // how to pop alert message to print the date I've chosen ?
});

How can I do this?

Share Improve this question edited Jan 7, 2014 at 0:01 Mark Amery 155k90 gold badges428 silver badges470 bronze badges asked Dec 6, 2013 at 0:35 hasan.alkhatibhasan.alkhatib 1,5593 gold badges16 silver badges29 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 17

As described in the docs, the Eternicode version of bootstrap-datepicker adds three attributes to the changeDate event that can be used for accessing the newly selected date. You'll be interested in either the .date property (which points to the actual date object representing the selected date) or the .format method, which you can use to get the date as a string.

Assuming the simplest case (where you have a single picker and you want to get the date as a string in the same format that you initialised the datepicker with), you'll just want to call .format() with no arguments:

$('#calendar').on('changeDate', function(event) {
    alert(event.format());
});

Here's a JSFiddle showing this in action: http://jsfiddle.net/bhm7p/3/

Put this code in the jquery function body:

$(document).ready(function () {
  $('#datepicker').on('dp.change', function(){
    var t=Date.parse($("#dob1").val());    
    if (t > Date.now()) {
      alert('Selected date shold not be a future date');
      $("#dob1").val('');
    }
  });
});

actually the fundamental is like this:
you will get access to the date by the .on event that parsed to the e parameter. from e you can't access to .getDate

$('#calendar').on('changeDate', function(e) {
    alert(e.getDate()+'/'+e.getMonth()+e.getFullYear());
});

if you can't understand how this happened, you can put the response in console.log(e) rather than alert(e.getDate) so you can get a clear view on what to access.

There are two things you could try.

Try implementing this line:

$('#startdate').val()

Or this one:

$('#startdate').data('date')

Drew T. suggests using .val() - also very efficient.

$('#calendar').on('changeDate', function(event, date) {
    var date = $('#calendar').val();
    alert(date);
});

Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论