Stretch0
Stretch0

Reputation: 9251

How to globally configure Moment to always format date as "DD MM"

I have a text input field and I want to try to assume the date the user is entering. I simply pass the text to a moment object and check is valid.

moment(textInput).isValid()

When a user enters 07 09 moment defaults to assuming it's month day which would be 09 July whereas I want moment to default to 07 September.

Is there a way to configure this moment to always assume the date is input as dd mm?

EDIT:

Have tried

const dateObj = moment("07 09", moment.defaultFormat).toDate()
console.log("dateObj", dateObj) // Sat Sep 01 2007 00:00:00 GMT+0100 (British Summer Time)

moment is very good at detecting what date you put in, whether it's 07 Sept, 07 09 etc. I still want it to accept any formats, I just want it to not assume the US format of dates and to always detect the first number as the date and the second as the month.

Upvotes: 4

Views: 788

Answers (3)

VincenzoC
VincenzoC

Reputation: 31482

There is no way to globally configure moment to make moment(String) parse input that are not ISO 8601 or RFC 2822. (If you want you can have a look at configFromString method in the source on GitHub page). Doc states that:

When creating a moment from a string, we first check if the string matches known ISO 8601 formats, we then check if the string matches the RFC 2822 Date time format before dropping to the fall back of new Date(string) if a known format is not found.

For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.

So, if you have to parse custom format you have to use moment(String, String) or moment(String, String[]):

If you don't know the exact format of an input string, but know it could be one of many, you can use an array of formats.

This is the same as String + Format, only it will try to match the input to multiple formats.

Please note that:

Starting in version 2.3.0, Moment uses some simple heuristics to determine which format to use. In order:

  • Prefer formats resulting in valid dates over invalid ones.
  • Prefer formats that parse more of the string than less and use more of the format than less, i.e. prefer stricter parsing.
  • Prefer formats earlier in the array than later.

In your case, you can use moment(String, String[]) passing "DD MM" and "DD MMM" in the array of formats to make moment parse both "07 09" and "07 Sept". Here a live sample:

const input = ["07 09", "07 Sept"];
const formats = ["DD MM", "DD MMM"];
input.forEach((value) => {
  let m = moment(value, formats)
  console.log("date", m.toDate());
  console.log("ISO string", m.format());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Upvotes: 2

Othmane
Othmane

Reputation: 289

This I think this solves the problem:

moment().format("DD MM"); 

Upvotes: 0

Hyyan Abo Fakher
Hyyan Abo Fakher

Reputation: 3527

You can use moment.defaultFormat and moment.defaultFormatUtc

See the doc format section

Upvotes: 1

Related Questions