Fichtelgard

Beautify Datestring

export function niceDateString(
dateString: string | undefined,
withHours: boolean = false,
withoutYear: boolean = false
) {
if (dateString === undefined) return "-";
const baseSplit = dateString.split("T");
if (baseSplit.length < 2) {
return unixToDateString(dateString);
} else if (withHours) {
const dateSplit = baseSplit[0].split("-");
if (dateSplit[0] === "1970") return "---";
const hourSplit = baseSplit[1].split(":");
const year = withoutYear ? "" : "." + dateSplit[0];
return (
dateSplit[2] +
"." +
dateSplit[1] +
year +
" - " +
hourSplit[0] +
":" +
hourSplit[1]
);
} else {
const dateSplit = baseSplit[0].split("-");
if (dateSplit[0] === "1970") return "---";
const year = withoutYear ? "" : "." + dateSplit[0];
return dateSplit[2] + "." + dateSplit[1] + year;
}
}