Reputation: 4539
I have 2 bootstrap 3 datepickers on a page. One is the issue date the other the due date. I want to set the due_date
to issue_date + transactionDaysTerms
(14
days for example).
$('#issue_date').datepicker({
}).on('changeDate', function(event){
$(this).datepicker('hide');
var calculatedEndDate = moment($('#issue_date').val()).add(transactionDaysTerms, 'days');
$('#issue_date').datepicker('setEndDate', calculatedEndDate);
$('#due_date').datepicker('setEndDate', calculatedEndDate);
}).on('clearDate', function(event){
$(this).datepicker('setDate', today);
});
I have tried various formats with moment
to no avail. This code fails with
Uncaught TypeError: date.match is not a function
How can I get the date formatting from moment.js to work with the datepicker?
Upvotes: 0
Views: 737
Reputation: 318182
Couldn't you just use getDate
to get the timestamp from the datepicker, and then multiply the days to get milliseconds, add it up, and set the end date
$('#issue_date').datepicker().on('changeDate', function(event) {
$(this).datepicker('hide');
var date = $(this).datepicker('getDate');
var days = transactionDaysTerms * 24 * 60 * 60 * 1000;
var calculatedEndDate = new Date( date.getTime() + days );
$('#issue_date, #due_date').datepicker('setEndDate', calculatedEndDate);
}).on('clearDate', function(event) {
$(this).datepicker('setDate', today);
});
Upvotes: 0
Reputation: 4539
This works perfectly.
$('#issue_date').datepicker({
}).on('changeDate', function(event){
$(this).datepicker('hide');
var calculatedEndDate = moment($(this).datepicker('getDate')).add(transactionDays, 'days').toDate();
$('#issue_date').datepicker('setEndDate', calculatedEndDate);
$('#due_date').datepicker('setDate', calculatedEndDate);
}).on('clearDate', function(event){
var calculatedEndDate = moment(today).add(transactionDays, 'days').format('L');
$('#issue_date').datepicker('setEndDate', calculatedEndDate);
$('#due_date').datepicker('setDate', calculatedEndDate);
});
I just needed .toDate()
at the end of the moment call.
Upvotes: 1