-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: day value not giving correct result in age field
- Loading branch information
1 parent
b0dc55c
commit 2cc70be
Showing
5 changed files
with
102 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/core_modules/capture-core/utils/converters/date/convertStringToTemporal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// @flow | ||
import { Temporal } from '@js-temporal/polyfill'; | ||
import { systemSettingsStore } from '../../../metaDataMemoryStores'; | ||
|
||
/** | ||
* Converts a date string into a Temporal.PlainDate object using the system set calendar | ||
* @export | ||
* @param {*} string - dateString | ||
* @returns {(Temporal.PlainDate | null)} | ||
*/ | ||
|
||
export function convertStringToTemporal(dateString: string): Temporal.PlainDate | null { | ||
if (!dateString) { | ||
return null; | ||
} | ||
try { | ||
const dateWithHyphen = dateString.replace(/[\/\.]/g, '-'); | ||
|
||
const calendar = systemSettingsStore.get().calendar; | ||
const dateFormat = systemSettingsStore.get().dateFormat; | ||
|
||
let year; let month; let day; | ||
|
||
if (dateFormat === 'YYYY-MM-DD') { | ||
[year, month, day] = dateWithHyphen.split('-').map(Number); | ||
} | ||
if (dateFormat === 'DD-MM-YYYY') { | ||
[day, month, year] = dateWithHyphen.split('-').map(Number); | ||
} | ||
return Temporal.PlainDate.from({ | ||
year, | ||
month, | ||
day, | ||
calendar, | ||
}); | ||
} catch (error) { | ||
return ''; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/core_modules/capture-core/utils/converters/date/convertTemporalToString.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// @flow | ||
import { Temporal } from '@js-temporal/polyfill'; | ||
import { padWithZeros } from './padWithZeros'; | ||
import { systemSettingsStore } from '../../../../capture-core/metaDataMemoryStores'; | ||
|
||
/** | ||
* Converts a Temporal.PlainDate to a formatted date string (YYYY-MM-DD || DD-MM-YYYY) | ||
* @param {Temporal.PlainDate} temporalDate - The Temporal date to convert | ||
* @returns {string} Formatted date string, or empty string if invalid | ||
*/ | ||
|
||
export function convertTemporalToString(temporalDate: Temporal.PlainDate | null): string { | ||
if (!temporalDate) { | ||
return ''; | ||
} | ||
const dateFormat = systemSettingsStore.get().dateFormat; | ||
|
||
try { | ||
const year = temporalDate.year; | ||
const month = temporalDate.month; | ||
const day = temporalDate.day; | ||
|
||
return dateFormat === 'YYYY-MM-DD' ? | ||
`${padWithZeros(year, 4)}-${padWithZeros(month, 2)}-${padWithZeros(day, 2)}` : | ||
`${padWithZeros(day, 2)}-${padWithZeros(month, 2)}-${padWithZeros(year, 4)}`; | ||
} catch (error) { | ||
return ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters