Date synchronization in jQuery without DateJS

I am trying to parse the format date January 1, 1900 or February 1, 1900 , etc. for all months .. and then separate the month, day and year from their own facilities.

I tried to use the regex because of the box for this, but:

  • This particular regex seems too complicated and as if it breaks easily
  • You need to use a simpler regular expression, knowing that the format will not change (and we will check the date on the server)

I don't want to use the DateJS library since it seems that a lot of code only includes parsing a single date, so is there an easier way to write a regex for this? Is there a route other than regular expressions or DateJS?

For some reason, the regular expression does not work in February, and as you can see, it returns a lot of objects in the array, while, obviously, it would be easier if it just returned 3 objects (month, day, year). Here is the current function that I wrote using the regex that I use ...:

 function convertDate(dateString) { // must be in the format MMMMMMM DD, YYYY OR MMM DD, YYYY // examples: January 1, 2000 or Jan 1, 2000 (notice no period for abbreviating January into Jan) var dateRegex = new RegExp('^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)\\ (0?[1-9]|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,\\ ((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))))\\,\\ ((1[6-9]|[2-9]\\d)\\d{2}))'); var fullDate = dateString.match(dateRegex); console.log(fullDate); if (fullDate) { var month = fullDate[12]; var day = fullDate[24]; var year = fullDate[35]; if (month == 'January' | month == 'Jan') { integerMonth = 1; } else if (month == 'February' | month == 'Feb') { integerMonth = 2; } else if (month == 'March' | month == 'Mar') { integerMonth = 3; } else if (month == 'April' | month == 'Apr') { integerMonth = 4; } else if (month == 'May') { integerMonth = 5; } else if (month == 'June' | month == 'Jun') { integerMonth = 6; } else if (month == 'July' | month == 'Jul') { integerMonth = 7; } else if (month == 'August' | month == 'Aug') { integerMonth = 8; } else if (month == 'September' | month == 'Sep') { integerMonth = 9; } else if (month == 'October' | month == 'Oct') { integerMonth = 10; } else if (month == 'November' | month == 'Nov') { integerMonth = 11; } else if (month == 'December' | month == 'Dec') { integerMonth = 12; } return {month : integerMonth, day : day, year : year} } else { return false; } } 
+4
source share
2 answers

The Javascript Date object can be initialized with a string and will process the format you use on the correct date:

 var d = new Date("January 1, 2000"); if (!isNaN(d.getMonth()) { // check for invalid date return {month : d.getMonth()+1, day : d.getDate(), year : d.getFullYear()}; } else { return false; } 

As you can see, this function is quite simple and should be supported in all modern browsers.

+5
source

This will work, but will not correspond to months and years. It just requires 3-9 letters, one or two numbers, one comma and 4 numbers.

 /^[az]{3,9} [0-9]{1,2}, [0-9]{4}$/i 
+1
source

Source: https://habr.com/ru/post/1340039/


All Articles