Skip to content

Commit

Permalink
Merge pull request #1732 from riganti/fix-globalizedate-errormsg
Browse files Browse the repository at this point in the history
Fix error message in dotvvm.formatString date parsing
  • Loading branch information
tomasherceg authored Nov 11, 2023
2 parents 1bbbe0a + 9f26146 commit a9bd0fa
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/Framework/Framework/Resources/Scripts/DotVVM.Globalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,34 @@ type GlobalizeFormattable = null | undefined | string | Date | number

export function formatString(format: string | null | undefined, value: GlobalizeFormattable | KnockoutObservable<GlobalizeFormattable>, type: string | null) {
value = ko.unwrap(value);
const originalValue = value;
if (value == null || value === "") {
return "";
}

if (format === "") {
format = null
}

if (typeof value === "string") {

// DateTime, DateOnly or TimeOnly
if (type === "dateonly") {
value = serializationParseDateOnly(value);
if (format == null || format.length == 0) {
format = "D";
}
format ??= "D"
} else if (type == "timeonly") {
value = serializationParseTimeOnly(value);
if (format == null || format.length == 0) {
format = "T";
}
format ??= "T"
} else {
value = serializationParseDate(value);
}

if (value == null) {
throw new Error(`Could not parse ${value} as a date`);
throw new Error(`Could not parse ${originalValue} as a ${type || "date"}`);
}
}

if (format == null || format.length == 0) {
format = "G";
}
format ??= "G"

return getGlobalize().format(value, format, getCulture());
}
Expand Down

0 comments on commit a9bd0fa

Please sign in to comment.