I'm trying to parse a csv file that may have different formats of dates, one file can have for example: "Data di fatturazione: 23.10.2023" another can have: 23.10.2023, another 23.10.23 and so on.
I need a way to loop when there is the parsing with papaparse and parse the date accordingly testing all the possibile formats.
Thank you!
I have tried this way, the "Data di fatturazione: " trim correctly, but other formats not work.
If I have only one item in the "possibileFormats" it work, but with other formats it give me invalid date and set to null.
//Using PapaParse to parse the combination of first and second row (headers and header data) while keeping the standard
const possibleFormats = ["yyyy.MM.dd HH:mm:ss", "dd.MM.yyyy HH:mm:ss"];
//@ts-ignore
const parsed = Papa.parse(fs.readFileSync(file.filepath, "utf-8"), {
header: true,
complete: (results) => {
return results;
},
transformHeader: (h: any) => {
//Header normalization
const normalizedHeader = ParserNormalizer.transformHeader(
h,
emptyHeaderCounter
);
// console.log("header", normalizedHeader);
if (normalizedHeader.wasEmpty) emptyHeaderCounter++;
return normalizedHeader.header;
},
skipEmptyLines: "greedy", //Greedy ignores empty lines even after parsing
dynamicTyping: true,
});
// console.log("parsed", parsed);
// Remove header row from CSV data
// const data = parsed.data.slice(1);
const parsedNormalizedReadings = parsed.data.map((row: any) => {
//Custom parsing depending on the fields of the selected rows
//device_id
const device_id = row[parsed.meta.fields![templateData.device_id!]];
//Billing Date Time
let billing_date_time =
row[parsed.meta.fields![templateData.billing_date_time!]];
let parsedDate = null;
if (billing_date_time?.includes("Data di fatturazione:")) {
parsedDate = parse(
billing_date_time.replace("Data di fatturazione:", "").trim(),
"dd.MM.yyyy",
new Date()
);
} else {
let dateFound = false;
for (const format of possibleFormats) {
console.log("data originale", billing_date_time);
parsedDate = parse(billing_date_time, format, new Date());
if (isValid(parsedDate)) {
dateFound = true;
console.log("parsed correctly");
break;
}
}
if (!dateFound) {
console.log("date not found");
parsedDate = null;
}
}
console.log("afterparse", parsedDate);
//billing_value
const billing_value =
row[parsed.meta.fields![templateData.billing_value_column!]];
//billing_value_extra
const billing_value_extra =
row[parsed.meta.fields![templateData.billing_value_extra_column!]];
//Returning the final record
return {
device_id,
parsedDate,
billing_value,
billing_value_extra,
};
});