Skip to content

Commit

Permalink
fix: download csv for swiss months
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Lauber <[email protected]>
  • Loading branch information
janlauber committed Nov 1, 2024
1 parent 4560367 commit 46a3e11
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions sk/src/routes/app/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,44 @@
});
}
function downloadCSV() {
const filteredExpenses = tempExpenses.filter((expense) => {
const date = new Date(expense.date);
return (
date.getMonth() + 1 === parseInt(selectedMonth) &&
date.getFullYear() === parseInt(selectedYear)
);
function downloadCSV(): void {
console.log(selectedMonth, selectedYear);
// Print the expenses for the selected month and year
console.log("Expenses for", selectedYear, selectedMonth);
// Define a mapping for German month abbreviations to full English equivalents
const germanToEnglishMonths: { [key: string]: string } = {
"Jan.": "Jan",
"Feb.": "Feb",
März: "Mar",
"Apr.": "Apr",
Mai: "May",
Juni: "Jun",
Juli: "Jul",
"Aug.": "Aug",
"Sept.": "Sep",
"Okt.": "Oct",
"Nov.": "Nov",
"Dez.": "Dec"
};
// Replace German month names with English equivalents before parsing
const normalizedDates = tempExpenses.map((expense: { date: string }) => {
let dateStr = expense.date;
Object.keys(germanToEnglishMonths).forEach((germanMonth) => {
dateStr = dateStr.replace(germanMonth, germanToEnglishMonths[germanMonth]);
});
return new Date(dateStr);
});
// Filter expenses for the selected month and year
const filteredExpenses = tempExpenses.filter((expense: { date: string }, index: number) => {
const date = normalizedDates[index];
return date.getMonth() + 1 === parseInt(selectedMonth) && date.getFullYear() === parseInt(selectedYear);
});
// Convert the filtered expenses to CSV
const csv = Papa.unparse(filteredExpenses);
const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
saveAs(blob, `expenses_${selectedYear}-${selectedMonth}.csv`);
Expand Down

0 comments on commit 46a3e11

Please sign in to comment.