wanasit / chrono

A natural language date parser in Javascript
MIT License
4.61k stars 341 forks source link

Forced american formatting #492

Closed kieran-s closed 1 year ago

kieran-s commented 1 year ago

Whenever I enter a date string (eg. 04/03/2022), it automatically defaults to the American formatting for parsing (mm/dd/yyyy).

How do I force it to use my regions formatting (dd/mm/yyyy) for when someone enters a date

wanasit commented 1 year ago

There are chrono.en.GB built-in variant:

chrono.parseDate('6/10/2018');            // June 10th, 2018
chrono.en.parseDate('6/10/2018');       // June 10th, 2018
chrono.en.GB.parseDate('6/10/2018');    // October 6th, 2018

If the built-in type doesn't work, you can follow the guide on Customizing Chrono.

import SlashDateFormatParser from "../src/common/parsers/SlashDateFormatParser";

const custom = chrono.en.strict.clone();

// Replace any existing SlashDateFormatParser with the little-endian one.
custom.parsers = custom.parsers.filter((r) => !(r instanceof SlashDateFormatParser));
custom.parsers.push(new SlashDateFormatParser(/*littleEndian=*/true));

custom.parseDate('6/10/2018');