0

Let's say I have a array of date strings,

const a = ["11/23/2018","10/22/1992","02/02/1993","01/01/1990",...];
const b = ["12/01/2018","12/12/1992","24/09/1993","16/01/1990",...];

is there way to identify data format of each of these arrays, like

a => MM/DD/YYYY
b => DD/MM/YYYY

in javascript. I am not looking for a solution in vanila javascript, if you can point me to a solution using momentjs or date-fns It would be fine.

thank you, in advance.

Sankha Karunasekara
  • 1,636
  • 18
  • 19
  • 4
    You could look at the month and day-of-month parts and check for number range to determine which is month and which is the day, but that will not always work for a bunch of dates that fall at the beginning of their months. – Pointy Dec 06 '18 at 13:17
  • How would you be able to do this, are you guaranteed that you have enough data to identify the format? But to give you an answer, I would use regex to see if theres among all the cases are any which has a month above 12, if so then its not a month.. – Martin M Dec 06 '18 at 13:19
  • @Pointy you could, but it would fail more than a third of the time. So I would say you really couldn't. – James Dec 06 '18 at 13:21
  • @James right, it's basically hopeless. – Pointy Dec 06 '18 at 13:24
  • There is no way to know if a person who typed in 1/3/2019 into a generic text field means march 1 or jan 3. – James Dec 06 '18 at 13:27
  • If you are sure at least one date on each group is higher than or equal to the 13rd day of the month you can. If all dates on a group are below day 13 then you'll be clueless. You need to be sure of that condition if you want it to always work, maybe that's something that is actually true on your system, no idea where are those groups comming from, but it really depends on that. – arieljuod Dec 06 '18 at 13:28
  • @arieljuod isn't that what has already been mentioned several times already? – charlietfl Dec 06 '18 at 13:29
  • The only other helper would be if you know the locale of the input source to improve your probability. – charlietfl Dec 06 '18 at 13:32
  • @charlietfl, I wanted to point that that it's not a plain "no" like some of the other users are saying because it looks they are talking about inspecting one single date instead of a group of dates and we don't actually know the context of that group of dates, I wanted to clarify that. – arieljuod Dec 06 '18 at 13:32
  • I am developing a bulk import functionlity, talking about atleast 100s of record in a single import. there will be enough dates to figure it out. I can obviously write some regex, but these records may have formats like "MMM DD, YYYY" or YYYY/MM/DD. but all records will be in same format. my question is there any library out there to figure out the format? – Sankha Karunasekara Dec 06 '18 at 13:49
  • if we don't have enough records to figure it out we can let user know and tell him to select a date format for us. – Sankha Karunasekara Dec 06 '18 at 13:52
  • @LakshanS have a look at momentjs [Parse Date Format](http://momentjs.com/docs/#/plugins/parseformat/) plug-in (but please note that asking for a library is off topic on SO). Anyway I agree with previous comments, there is no way to get the format for input like `1/2/2017`. – VincenzoC Dec 06 '18 at 15:20
  • Thank you all. @vincenzoC I am sorry if I am not clear, I am not asking to format a single date like 1/2/2017. if we got 1/2/2017 & 23/2/2017 in the same array we definitly know, format is DD/MM/YYYY. – Sankha Karunasekara Dec 06 '18 at 15:37

1 Answers1

1

As stated in the comments, you can use momentjs Parse Date Format plug-in.

This plug-in add the parseFormat method:

That allows to create smart date inputs that let your users set a Date/Time and lets you extract the user's preferred format for future usage.

parseFormat method accepts preferredOrder options to customize plug-in results for ambigous inputs. You can use preferredOrder and your own logic to get your desired result.

Here an example of a function that returns the most frequent format in the array with extracted with default parseFormat behaviour:

const a = ["11/23/2018","10/22/1992","02/02/1993","01/01/1990"];
const b = ["12/01/2018","12/12/1992","24/09/1993","16/01/1990"];

function getFormat(arr){
  return arr.map((elem) => moment.parseFormat(elem))
       // get most frequent element from here: https://stackoverflow.com/a/20762713/4131048
        .sort((a,b) =>
          arr.filter(v => v===a).length
        - arr.filter(v => v===b).length
        ).pop();
}

console.log( getFormat(a) );
console.log( getFormat(b) );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>
<script src="https://gr2m.github.io/moment-parseformat/moment-parseformat.js"></script>
VincenzoC
  • 30,117
  • 12
  • 90
  • 112