How to globally configure Moment to always format date as “DD MM”

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.


07 09


month day


09 July


moment


07 September



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


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.


moment


07 Sept


07 09




3 Answers
3



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:


moment(String)


configFromString



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.


new Date(string)



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):


moment(String, String)


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:



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:


moment(String, String)


"DD MM"


"DD MMM"


"07 09"


"07 Sept"




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>





Thanks for the detailed response. Passing in multiple date formats has done the trick.
– Stretch0
Aug 22 at 9:20





Glad my answer helped you! @downvoter can you share what you think is wrong in my answer? It's quite strange to get downvote for a detailed accepted answer.
– VincenzoC
Aug 22 at 13:35





@VincenzoC I overlooked the answer and downvoted, reading this again seems pretty good and correct, could you please edit something in your answer, "I can see see some typos", so I can upvote again for the answer, sorry :)
– Hyyan Abo Fakher
Aug 22 at 14:16






@HyyanAboFakher thank you for explaining the reason of downvote, I've edited my answer.
– VincenzoC
Aug 22 at 14:20





@VincenzoC upvoted , good answer
– Hyyan Abo Fakher
Aug 22 at 14:21



You can use moment.defaultFormat and moment.defaultFormatUtc


moment.defaultFormat


moment.defaultFormatUtc



See the doc format section





I tried this to no avail. Have updated my OP to explain where it's going wrong.
– Stretch0
Aug 21 at 14:27



This I think this solves the problem:


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





Unfortunately not. That just formats the date that gets passed into the moment object. i.e. const dateObj = moment("07 09").format("DD MM") console.log(dateObj) outputs 09 07. I still want the moment date object returned, I just want moment to know that the date comes first and the month comes second.
– Stretch0
Aug 21 at 14:39


const dateObj = moment("07 09").format("DD MM") console.log(dateObj)


09 07






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)