From 21d259de5892df2d286b542b80c5225c7c0436ce Mon Sep 17 00:00:00 2001 From: dreaming-augustin Date: Thu, 26 Dec 2024 21:27:33 +0800 Subject: [PATCH] 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. --- src/definitions/modules/calendar.js | 38 +++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/definitions/modules/calendar.js b/src/definitions/modules/calendar.js index 7ff6b4dfd5..925099d75d 100644 --- a/src/definitions/modules/calendar.js +++ b/src/definitions/modules/calendar.js @@ -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; @@ -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;