Skip to content

Commit

Permalink
feat(calendar): "get date" can return formatted date
Browse files Browse the repository at this point in the history
$('.ui.calendar').calendar('get date');
$('.ui.calendar').calendar('get focusDate');
$('.ui.calendar').calendar('get startDate');
$('.ui.calendar').calendar('get endDate');

could so far only return a Javascript Date object.

Now, given a format argument, they return a formatted date string:

$('.ui.calendar').calendar('get date',      "YYYY-MM-DD");
$('.ui.calendar').calendar('get focusDate', "YYYY-MM-DD");
$('.ui.calendar').calendar('get startDate', "YYYY-MM-DD");
$('.ui.calendar').calendar('get endDate',   "YYYY-MM-DD");

The format string is passed to the existing formatter.

This is backward compatible as the default behaviour does not change.

It makes it convenient for developers to get exactly what they need when they need it.
  • Loading branch information
dreaming-augustin authored Dec 26, 2024
1 parent 22d2d0d commit 21d259d
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions src/definitions/modules/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,24 +802,32 @@
formattedDate: function (format, date) {
return module.helper.dateFormat(format || formatter[settings.type], date || module.get.date());
},
date: function () {
return module.helper.sanitiseDate($module.data(metadata.date)) || null;
date: function (format) {
return module.helper.dateObjectOrFormatted(format, $module.data(metadata.date));
},
inputDate: function () {
return $input.val();
},
focusDate: function () {
return $module.data(metadata.focusDate) || null;
focusDate: function (format) {
return module.helper.dateObjectOrFormatted(format, $module.data(metadata.focusDate));
},
startDate: function () {
startDate: function (format) {
var startModule = module.get.calendarModule(settings.startCalendar);

return (startModule ? startModule.get.date() : $module.data(metadata.startDate)) || null;
if (startModule) {
return startModule.get.date(format);
}

return module.helper.dateObjectOrFormatted(format, $module.data(metadata.startDate));
},
endDate: function () {
endDate: function (format) {
var endModule = module.get.calendarModule(settings.endCalendar);

return (endModule ? endModule.get.date() : $module.data(metadata.endDate)) || null;
if (endModule) {
return endModule.get.date(format);
}

return module.helper.dateObjectOrFormatted(format, $module.data(metadata.endDate));
},
minDate: function () {
return $module.data(metadata.minDate) || null;
Expand Down Expand Up @@ -1135,6 +1143,20 @@
return match.slice(1, -1);
});
},
dateObjectOrFormatted: function (format, date) {
format = format || '';
date = module.helper.sanitiseDate(date) || null;

if (!date) {
return null;
}

if (format === '') {
return date;
}

return module.helper.dateFormat(format, date);
},
isDisabled: function (date, mode) {
return (mode === 'day' || mode === 'month' || mode === 'year' || mode === 'hour') && (((mode === 'day' && settings.disabledDaysOfWeek.indexOf(date.getDay()) !== -1) || settings.disabledDates.some(function (d) {
var blocked = false;
Expand Down

0 comments on commit 21d259d

Please sign in to comment.