Skip to content

Commit

Permalink
Format date properties
Browse files Browse the repository at this point in the history
  • Loading branch information
schiwekM committed Oct 7, 2024
1 parent 457a2a0 commit 143cf79
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.cds
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ annotate ChangeView with @(UI: {
RequestAtLeast: [
parentKey,
serviceEntity,
serviceEntityPath
serviceEntityPath,
valueDataType
],
SortOrder : [{
Property : createdAt,
Expand Down
15 changes: 15 additions & 0 deletions lib/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,28 @@ const _getLabelI18nKeyOnEntity = function (entityName, /** optinal */ attribute)
return def['@Common.Label'] || def['@title'];
};

const dateFormatOptions = {
year: 'numeric',
month: 'short',
day: 'numeric',
}
const _localizeDates = (change, locale) => {
if (change.valueDataType === 'cds.Date') {
if (change.valueChangedFrom)
change.valueChangedFrom = new Date(change.valueChangedFrom).toLocaleDateString(locale.replaceAll('_', '-'), dateFormatOptions) //locale.replace because en_GB is unknown to function and it has to be en-GB
if (change.valueChangedTo)
change.valueChangedTo = new Date(change.valueChangedTo).toLocaleDateString(locale.replaceAll('_', '-'), dateFormatOptions)
}
}

const localizeLogFields = function (data, locale) {
if (!locale) return
for (const change of data) {
_localizeModification(change, locale);
_localizeAttribute(change, locale);
_localizeEntityType(change, locale);
_localizeDefaultObjectID(change, locale);
_localizeDates(change, locale);
}
};
module.exports = {
Expand Down

0 comments on commit 143cf79

Please sign in to comment.