Skip to content

Commit df9c6aa

Browse files
committed
fix: download csv for swiss months
Signed-off-by: Jan Lauber <[email protected]>
1 parent 4560367 commit df9c6aa

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

sk/src/routes/app/+page.svelte

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,47 @@
106106
});
107107
}
108108
109-
function downloadCSV() {
110-
const filteredExpenses = tempExpenses.filter((expense) => {
111-
const date = new Date(expense.date);
109+
function downloadCSV(): void {
110+
console.log(selectedMonth, selectedYear);
111+
112+
// Print the expenses for the selected month and year
113+
console.log("Expenses for", selectedYear, selectedMonth);
114+
115+
// Define a mapping for German month abbreviations to full English equivalents
116+
const germanToEnglishMonths: { [key: string]: string } = {
117+
"Jan.": "Jan",
118+
"Feb.": "Feb",
119+
März: "Mar",
120+
"Apr.": "Apr",
121+
Mai: "May",
122+
Juni: "Jun",
123+
Juli: "Jul",
124+
"Aug.": "Aug",
125+
"Sept.": "Sep",
126+
"Okt.": "Oct",
127+
"Nov.": "Nov",
128+
"Dez.": "Dec"
129+
};
130+
131+
// Replace German month names with English equivalents before parsing
132+
const normalizedDates = tempExpenses.map((expense: { date: string }) => {
133+
let dateStr = expense.date;
134+
Object.keys(germanToEnglishMonths).forEach((germanMonth) => {
135+
dateStr = dateStr.replace(germanMonth, germanToEnglishMonths[germanMonth]);
136+
});
137+
return new Date(dateStr);
138+
});
139+
140+
// Filter expenses for the selected month and year
141+
const filteredExpenses = tempExpenses.filter((expense: { date: string }, index: number) => {
142+
const date = normalizedDates[index];
112143
return (
113144
date.getMonth() + 1 === parseInt(selectedMonth) &&
114145
date.getFullYear() === parseInt(selectedYear)
115146
);
116147
});
148+
149+
// Convert the filtered expenses to CSV
117150
const csv = Papa.unparse(filteredExpenses);
118151
const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
119152
saveAs(blob, `expenses_${selectedYear}-${selectedMonth}.csv`);

0 commit comments

Comments
 (0)