diff options
author | xiaoxintang <longyan.xxt@raycloud.com> | 2020-12-12 21:21:48 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-12 14:21:48 +0100 |
commit | 89c14f79a45868c8f94eab97d794f8a27096a154 (patch) | |
tree | ebf61b6716b3b8c9069e79478b1abf53a9dbcfa6 /std/datetime/formatter.ts | |
parent | 93cd9ab0b83e0df644315e7303ffb6280307e723 (diff) |
fix(std/datetime): partsToDate (#8553)
Diffstat (limited to 'std/datetime/formatter.ts')
-rw-r--r-- | std/datetime/formatter.ts | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/std/datetime/formatter.ts b/std/datetime/formatter.ts index 8b9c9d9b0..ea16dbe0d 100644 --- a/std/datetime/formatter.ts +++ b/std/datetime/formatter.ts @@ -511,6 +511,28 @@ export class DateTimeFormatter { return parts; } + /** sort & filter dateTimeFormatPart */ + sortDateTimeFormatPart(parts: DateTimeFormatPart[]): DateTimeFormatPart[] { + let result: DateTimeFormatPart[] = []; + const typeArray = [ + "year", + "month", + "day", + "hour", + "minute", + "second", + "fractionalSecond", + ]; + for (const type of typeArray) { + const current = parts.findIndex((el) => el.type === type); + if (current !== -1) { + result = result.concat(parts.splice(current, 1)); + } + } + result = result.concat(parts); + return result; + } + partsToDate(parts: DateTimeFormatPart[]): Date { const date = new Date(); const utc = parts.find( @@ -566,6 +588,7 @@ export class DateTimeFormatter { parse(string: string): Date { const parts = this.parseToParts(string); - return this.partsToDate(parts); + const sortParts = this.sortDateTimeFormatPart(parts); + return this.partsToDate(sortParts); } } |