-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
16 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
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
32 changes: 22 additions & 10 deletions
32
packages/backend/src/usagers/services/xlsx-structure-usagers-renderer/applyDateFormat.ts
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 |
---|---|---|
@@ -1,17 +1,29 @@ | ||
import { StructureCustomDocTags } from "../../../_common/model"; | ||
import { isValid, parse } from "date-fns"; | ||
import { parse } from "date-fns"; | ||
|
||
export const applyDateFormat = ( | ||
worksheet: StructureCustomDocTags[], | ||
elements: Array<keyof StructureCustomDocTags> | ||
): void => { | ||
worksheet.forEach((ws: StructureCustomDocTags) => { | ||
elements.forEach((element: keyof StructureCustomDocTags) => { | ||
if (ws[element]) { | ||
const value = ws[element] as string; | ||
ws[element] = isValid(parse(value, "dd/MM/yyyy", new Date())) | ||
? parse(value, "dd/MM/yyyy", new Date()) | ||
: value; | ||
const baseDate = new Date(); | ||
const dateFormat = "dd/MM/yyyy"; | ||
|
||
for (let i = 0; i < worksheet.length; i++) { | ||
const ws = worksheet[i]; | ||
|
||
for (let j = 0; j < elements.length; j++) { | ||
const element = elements[j]; | ||
const value = ws[element]; | ||
|
||
if (!value) continue; | ||
|
||
try { | ||
const parsedDate = parse(value as string, dateFormat, baseDate); | ||
ws[element] = | ||
parsedDate.toString() !== "Invalid Date" ? parsedDate : value; | ||
} catch { | ||
continue; | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
}; |