Skip to content

Commit 21d259d

Browse files
feat(calendar): "get date" can return formatted date
$('.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.
1 parent 22d2d0d commit 21d259d

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/definitions/modules/calendar.js

+30-8
Original file line numberDiff line numberDiff line change
@@ -802,24 +802,32 @@
802802
formattedDate: function (format, date) {
803803
return module.helper.dateFormat(format || formatter[settings.type], date || module.get.date());
804804
},
805-
date: function () {
806-
return module.helper.sanitiseDate($module.data(metadata.date)) || null;
805+
date: function (format) {
806+
return module.helper.dateObjectOrFormatted(format, $module.data(metadata.date));
807807
},
808808
inputDate: function () {
809809
return $input.val();
810810
},
811-
focusDate: function () {
812-
return $module.data(metadata.focusDate) || null;
811+
focusDate: function (format) {
812+
return module.helper.dateObjectOrFormatted(format, $module.data(metadata.focusDate));
813813
},
814-
startDate: function () {
814+
startDate: function (format) {
815815
var startModule = module.get.calendarModule(settings.startCalendar);
816816

817-
return (startModule ? startModule.get.date() : $module.data(metadata.startDate)) || null;
817+
if (startModule) {
818+
return startModule.get.date(format);
819+
}
820+
821+
return module.helper.dateObjectOrFormatted(format, $module.data(metadata.startDate));
818822
},
819-
endDate: function () {
823+
endDate: function (format) {
820824
var endModule = module.get.calendarModule(settings.endCalendar);
821825

822-
return (endModule ? endModule.get.date() : $module.data(metadata.endDate)) || null;
826+
if (endModule) {
827+
return endModule.get.date(format);
828+
}
829+
830+
return module.helper.dateObjectOrFormatted(format, $module.data(metadata.endDate));
823831
},
824832
minDate: function () {
825833
return $module.data(metadata.minDate) || null;
@@ -1135,6 +1143,20 @@
11351143
return match.slice(1, -1);
11361144
});
11371145
},
1146+
dateObjectOrFormatted: function (format, date) {
1147+
format = format || '';
1148+
date = module.helper.sanitiseDate(date) || null;
1149+
1150+
if (!date) {
1151+
return null;
1152+
}
1153+
1154+
if (format === '') {
1155+
return date;
1156+
}
1157+
1158+
return module.helper.dateFormat(format, date);
1159+
},
11381160
isDisabled: function (date, mode) {
11391161
return (mode === 'day' || mode === 'month' || mode === 'year' || mode === 'hour') && (((mode === 'day' && settings.disabledDaysOfWeek.indexOf(date.getDay()) !== -1) || settings.disabledDates.some(function (d) {
11401162
var blocked = false;

0 commit comments

Comments
 (0)