diff --git a/src/core_modules/capture-core/converters/clientToForm.js b/src/core_modules/capture-core/converters/clientToForm.js index c0b0d10948..d8cec38fd3 100644 --- a/src/core_modules/capture-core/converters/clientToForm.js +++ b/src/core_modules/capture-core/converters/clientToForm.js @@ -23,17 +23,14 @@ type RangeValue = { } function convertDateForEdit(rawValue: string): string { - const momentDate = moment(rawValue); - const dateString = momentDate.format('YYYY-MM-DD'); - return convertIsoToLocalCalendar(dateString); + return convertIsoToLocalCalendar(rawValue); } function convertDateTimeForEdit(rawValue: string): DateTimeFormValue { const dateTime = moment(rawValue); - const dateString = dateTime.format('YYYY-MM-DD'); const timeString = dateTime.format('HH:mm'); return { - date: convertIsoToLocalCalendar(dateString), + date: convertIsoToLocalCalendar(rawValue), time: timeString, }; } diff --git a/src/core_modules/capture-core/converters/clientToList.js b/src/core_modules/capture-core/converters/clientToList.js index 6ee3cb78a7..f026191a4e 100644 --- a/src/core_modules/capture-core/converters/clientToList.js +++ b/src/core_modules/capture-core/converters/clientToList.js @@ -11,16 +11,13 @@ import { MinimalCoordinates } from '../components/MinimalCoordinates'; import { TooltipOrgUnit } from '../components/Tooltips/TooltipOrgUnit'; function convertDateForListDisplay(rawValue: string): string { - const momentDate = moment(rawValue); - const dateString = momentDate.format('YYYY-MM-DD'); - return convertIsoToLocalCalendar(dateString); + return convertIsoToLocalCalendar(rawValue); } function convertDateTimeForListDisplay(rawValue: string): string { const momentDate = moment(rawValue); - const dateString = momentDate.format('YYYY-MM-DD'); const timeString = momentDate.format('HH:mm'); - const localDate = convertIsoToLocalCalendar(dateString); + const localDate = convertIsoToLocalCalendar(rawValue); return `${localDate} ${timeString}`; } diff --git a/src/core_modules/capture-core/converters/clientToView.js b/src/core_modules/capture-core/converters/clientToView.js index 6f4e2b7e14..0eb7efc21e 100644 --- a/src/core_modules/capture-core/converters/clientToView.js +++ b/src/core_modules/capture-core/converters/clientToView.js @@ -11,16 +11,13 @@ import { TooltipOrgUnit } from '../components/Tooltips/TooltipOrgUnit'; function convertDateForView(rawValue: string): string { - const momentDate = moment(rawValue); - const dateString = momentDate.format('YYYY-MM-DD'); - return convertIsoToLocalCalendar(dateString); + return convertIsoToLocalCalendar(rawValue); } function convertDateTimeForView(rawValue: string): string { const momentDate = moment(rawValue); - const dateString = momentDate.format('YYYY-MM-DD'); const timeString = momentDate.format('HH:mm'); - const localDate = convertIsoToLocalCalendar(dateString); + const localDate = convertIsoToLocalCalendar(rawValue); return `${localDate} ${timeString}`; } diff --git a/src/core_modules/capture-core/converters/formToClient.js b/src/core_modules/capture-core/converters/formToClient.js index 61b37923fd..d0a4460e24 100644 --- a/src/core_modules/capture-core/converters/formToClient.js +++ b/src/core_modules/capture-core/converters/formToClient.js @@ -38,12 +38,8 @@ function convertDateTime(formValue: DateTimeValue): ?string { function convertDate(dateValue: string) { const parsedDate = parseDate(dateValue); - if (!parsedDate.isValid || !parsedDate.momentDate) { - return null; - } - const formattedDate = parsedDate.momentDate.format('YYYY-MM-DD'); - - return convertLocalToIsoCalendar(formattedDate); + // $FlowFixMe[incompatible-use] automated comment + return parsedDate.isValid ? convertLocalToIsoCalendar(parsedDate.momentDate.toISOString()) : null; } function convertTime(timeValue: string) { diff --git a/src/core_modules/capture-core/utils/converters/date/convertLocalToIsoCalendar.js b/src/core_modules/capture-core/utils/converters/date/convertLocalToIsoCalendar.js index b3f323d178..2c2ce3d192 100644 --- a/src/core_modules/capture-core/utils/converters/date/convertLocalToIsoCalendar.js +++ b/src/core_modules/capture-core/utils/converters/date/convertLocalToIsoCalendar.js @@ -16,9 +16,17 @@ export function convertLocalToIsoCalendar(localDate: ?string): string { if (!localDate) { return ''; } + + const momentDate = moment(localDate); + if (!momentDate.isValid()) { + return ''; + } + + const formattedIsoDate = momentDate.format('YYYY-MM-DD'); + const calendar = systemSettingsStore.get().calendar; - const { year, month, day } = convertToIso8601(localDate, calendar); + const { year, month, day } = convertToIso8601(formattedIsoDate, calendar); const dateString = `${padWithZeros(year, 4)}-${padWithZeros(month, 2)}-${padWithZeros(day, 2)}`; const parsedMoment = moment(dateString); diff --git a/src/core_modules/capture-core/utils/converters/date/stringToMomentDateFormat.js b/src/core_modules/capture-core/utils/converters/date/stringToMomentDateFormat.js index 1e718a5119..29c7b2c929 100644 --- a/src/core_modules/capture-core/utils/converters/date/stringToMomentDateFormat.js +++ b/src/core_modules/capture-core/utils/converters/date/stringToMomentDateFormat.js @@ -6,9 +6,10 @@ import { systemSettingsStore } from '../../../metaDataMemoryStores'; * Converts a string date to a string date with default format based on the system date format * @export * @param {*} string - the string instance + * @param {string} [format] - optional date format. If not provided, the function uses system date format * @returns {string} */ -export function convertStringToDateFormat(date: string, format) { +export function convertStringToDateFormat(date: string, format?: string) { if (!date || !date.length) { return ''; } const dateFormat = format || systemSettingsStore.get().dateFormat; const formattedDateString = moment(date, dateFormat).format(dateFormat); diff --git a/src/core_modules/capture-ui/DateAndTimeFields/DateField/Date.component.js b/src/core_modules/capture-ui/DateAndTimeFields/DateField/Date.component.js index c8ba299197..1c4b2ee4b0 100644 --- a/src/core_modules/capture-ui/DateAndTimeFields/DateField/Date.component.js +++ b/src/core_modules/capture-ui/DateAndTimeFields/DateField/Date.component.js @@ -65,7 +65,6 @@ export class DateField extends React.Component { value, innerMessage, } = this.props; -console.log(calendarMax,"calendarMax") const calculatedInputWidth = inputWidth || width; const calculatedCalendarWidth = calendarWidth || width; const calendarType = systemSettingsStore.get().calendar || 'gregory';