(core) Use YYYY-MM-DD as default date format for parsing

Summary: By default, new date columns show "YYYY-MM-DD" as the date format in the UI, but under the hood it's stored as null/undefined, which affects parsing during pasting and type conversion. This changes the underlying parsing to always default to YYYY-MM-DD, as if that format was explicitly selected, making things more consistent.

Test Plan: Updated some tests.

Reviewers: georgegevoian

Reviewed By: georgegevoian

Subscribers: dsagal

Differential Revision: https://phab.getgrist.com/D3265
This commit is contained in:
Alex Hall 2022-02-14 16:56:24 +02:00
parent afa90cc365
commit 8b5e837d9c

View File

@ -104,11 +104,8 @@ export function parseDate(date: string, options: ParseOptions = {}): number | nu
if (!date) {
return null;
}
const dateFormats = PARSER_FORMATS.slice();
// If a preferred parse format is given, set that to be the first parser used.
if (options.dateFormat) {
dateFormats.unshift(..._buildVariations(options.dateFormat, date));
}
const dateFormat = options.dateFormat || "YYYY-MM-DD";
const dateFormats = [..._buildVariations(dateFormat, date), ...PARSER_FORMATS];
const cleanDate = date.replace(SEPARATORS, ' ');
let datetime = cleanDate.trim();
let timeformat = '';
@ -148,11 +145,8 @@ export function parseDateStrict(
if (!date) {
return;
}
const dateFormats = [];
if (dateFormat) {
dateFormats.push(..._buildVariations(dateFormat, date));
}
dateFormats.push(...UNAMBIGUOUS_FORMATS);
dateFormat = dateFormat || "YYYY-MM-DD";
const dateFormats = [..._buildVariations(dateFormat, date), ...UNAMBIGUOUS_FORMATS];
const cleanDate = date.replace(SEPARATORS, ' ').trim();
for (const format of dateFormats) {
const m = moment.tz(cleanDate, format, true, timezone);
@ -173,7 +167,7 @@ export function parseDateTime(dateTime: string, options: ParseOptions): number |
return;
}
const dateFormat = options.dateFormat || null;
const dateFormat = options.dateFormat || "YYYY-MM-DD";
const timezone = options.timezone || "UTC";
const dateOnly = parseDateStrict(dateTime, dateFormat, undefined, timezone);