parse csv date in ISO format (#1303)

* Handle various date formats

* Update changelog

Co-authored-by: Thomas <4159106+dtslvr@users.noreply.github.com>
This commit is contained in:
Mitchell 2022-10-28 11:26:19 +02:00 committed by GitHub
parent 5c6cc4fed5
commit fca0a688b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View File

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Added support for translated labels of asset and asset sub class - Added support for translated labels of asset and asset sub class
- Added support for dates in _ISO 8601_ date format (`YYYY-MM-DD`) in the activities import
### Fixed ### Fixed

View File

@ -2,7 +2,7 @@ import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { CreateOrderDto } from '@ghostfolio/api/app/order/create-order.dto'; import { CreateOrderDto } from '@ghostfolio/api/app/order/create-order.dto';
import { Account, DataSource, Type } from '@prisma/client'; import { Account, DataSource, Type } from '@prisma/client';
import { parse } from 'date-fns'; import { isMatch, parse, parseISO } from 'date-fns';
import { isFinite } from 'lodash'; import { isFinite } from 'lodash';
import { parse as csvToJson } from 'papaparse'; import { parse as csvToJson } from 'papaparse';
import { EMPTY } from 'rxjs'; import { EMPTY } from 'rxjs';
@ -153,13 +153,15 @@ export class ImportTransactionsService {
for (const key of ImportTransactionsService.DATE_KEYS) { for (const key of ImportTransactionsService.DATE_KEYS) {
if (item[key]) { if (item[key]) {
try { if (isMatch(item[key], 'dd-MM-yyyy')) {
date = parse(item[key], 'dd-MM-yyyy', new Date()).toISOString(); date = parse(item[key], 'dd-MM-yyyy', new Date()).toISOString();
} catch {} } else if (isMatch(item[key], 'dd/MM/yyyy')) {
try {
date = parse(item[key], 'dd/MM/yyyy', new Date()).toISOString(); date = parse(item[key], 'dd/MM/yyyy', new Date()).toISOString();
} catch {} } else {
try {
date = parseISO(item[key]).toISOString();
} catch {}
}
if (date) { if (date) {
return date; return date;