Repository navigation
[Payment due @brunovjk] Allow several date formats for CSV company cards #91933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
126c198
44e5e90
7926d9d
703b07e
eca450e
4d2d879
bcec5e1
4d772bb
caf0a6b
6a87dc3
4ca74c2
6257f01
c78b0d2
d6bc799
fb0f8c9
e67d3ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,8 @@ | ||
| import {addDays, format, isValid, parse} from 'date-fns'; | ||
| import CONST from '@src/CONST'; | ||
|
|
||
| /** | ||
| * Common date formats to try when parsing CSV dates. Order matters: more | ||
| * specific / more common formats are tried first. | ||
| */ | ||
| // Common date formats to try when parsing CSV dates | ||
| // Order matters - more specific/common formats first | ||
| const CSV_DATE_FORMATS = [ | ||
| 'yyyy-MM-dd', // ISO format: 2025-11-02 | ||
| 'MM/dd/yyyy', // US format: 11/02/2025 | ||
|
|
@@ -22,13 +20,7 @@ const CSV_DATE_FORMATS = [ | |
| ]; | ||
|
|
||
| /** | ||
| * Parses a date string from a variety of common CSV formats and returns it in | ||
| * `yyyy-MM-dd`. Returns null when the input cannot be parsed as a date. | ||
| * | ||
| * The yyyy-MM-dd format is tried via `date-fns.parse` before falling back to | ||
| * the native Date constructor so that bare ISO dates (e.g. `2024-01-15`) are | ||
| * interpreted in the local timezone instead of being shifted by UTC midnight | ||
| * in zones west of UTC. | ||
| * Parses a date string from various formats and returns it in yyyy-MM-dd format | ||
| */ | ||
| function parseCSVDate(input: string): string | null { | ||
| if (!input || typeof input !== 'string') { | ||
|
|
@@ -37,48 +29,44 @@ function parseCSVDate(input: string): string | null { | |
|
|
||
| const trimmedInput = input.trim(); | ||
|
|
||
| // Try the explicit formats first. The native `Date` constructor parses | ||
| // bare ISO dates (e.g. "2024-01-15") as UTC midnight, which would shift to | ||
| // the previous day in any zone west of UTC once formatted back. The | ||
| // format list starts with `yyyy-MM-dd` so those inputs are interpreted in | ||
| // local time and round-trip cleanly. | ||
| // Try native Date parsing first (handles ISO and some other formats) | ||
| let date = new Date(trimmedInput); | ||
| if (isValid(date) && !Number.isNaN(date.getTime())) { | ||
| return format(date, CONST.DATE.FNS_FORMAT_STRING); | ||
| } | ||
|
|
||
| // Try parsing with common date formats using date-fns | ||
| for (const dateFormat of CSV_DATE_FORMATS) { | ||
| const parsedDate = parse(trimmedInput, dateFormat, new Date()); | ||
| if (isValid(parsedDate)) { | ||
| return format(parsedDate, CONST.DATE.FNS_FORMAT_STRING); | ||
| } | ||
| } | ||
|
|
||
| // Fall back to the native Date constructor for anything else (ISO 8601 | ||
| // date-time strings, RFC 2822, etc). | ||
| let date = new Date(trimmedInput); | ||
| if (isValid(date) && !Number.isNaN(date.getTime())) { | ||
| return format(date, CONST.DATE.FNS_FORMAT_STRING); | ||
| } | ||
|
|
||
| // If still unparsed, try just the first 10 characters in case the input | ||
| // is a longer string with a date prefix. | ||
| // If the date didn't parse, try taking just the first 10 characters | ||
| if (trimmedInput.length > 10) { | ||
| const shortInput = trimmedInput.substring(0, 10); | ||
| date = new Date(shortInput); | ||
| if (isValid(date) && !Number.isNaN(date.getTime())) { | ||
| return format(date, CONST.DATE.FNS_FORMAT_STRING); | ||
| } | ||
|
|
||
| // Also try format parsing on the shortened input | ||
|
Gonals marked this conversation as resolved.
Resolved
|
||
| for (const dateFormat of CSV_DATE_FORMATS) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ CONSISTENCY-3 (docs)The for-loop that iterates over Suggested fix -- extract a helper function: function tryParseWithFormats(dateString: string): string | null {
for (const dateFormat of CSV_DATE_FORMATS) {
const parsedDate = parse(dateString, dateFormat, new Date());
if (isValid(parsedDate)) {
return format(parsedDate, CONST.DATE.FNS_FORMAT_STRING);
}
}
return null;
}Then call Reviewed at: bcec5e1 | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAB: Do you think it's worth making a change here in this PR, or perhaps in a follow-up? cc: @Gonals
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine. It is just a handful of lines 🤷 |
||
| const parsedDate = parse(shortInput, dateFormat, new Date()); | ||
| if (isValid(parsedDate)) { | ||
| return format(parsedDate, CONST.DATE.FNS_FORMAT_STRING); | ||
| } | ||
| } | ||
| date = new Date(shortInput); | ||
| if (isValid(date) && !Number.isNaN(date.getTime())) { | ||
| return format(date, CONST.DATE.FNS_FORMAT_STRING); | ||
| } | ||
| } | ||
|
|
||
| // Maybe it's an Excel serial date number. Excel stores dates serialized | ||
| // from January 1st 1900 (with 1/1/1900 being 1), and incorrectly treats | ||
| // 1900 as a leap year, so we subtract 2 days when converting. | ||
| // If it didn't parse, maybe it's an Excel date number | ||
| // Excel stores dates serialized from January 1st, 1900 (with 1/1/1900 being 1) | ||
| // Excel thinks that 1900 was a leap year and adds an extra day to account for that | ||
| if (/^\d+$/.test(trimmedInput)) { | ||
| const inputInt = parseInt(trimmedInput, 10); | ||
| if (inputInt > 0 && inputInt < 100000) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ CONSISTENCY-2 (docs)The magic number Suggested fix: // Excel serial date 100000 corresponds to ~2173-10-14, well beyond any reasonable CSV date
const MAX_EXCEL_SERIAL_DATE = 100000;Then use Reviewed at: bcec5e1 | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Same here @Gonals
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. I think this is fine 🤷 |
||
| const excelEpoch = new Date(1900, 0, 1); | ||
| const excelEpoch = new Date(1900, 0, 1); // January 1, 1900 | ||
| const parsedDate = addDays(excelEpoch, inputInt - 2); | ||
| if (isValid(parsedDate)) { | ||
| return format(parsedDate, CONST.DATE.FNS_FORMAT_STRING); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.