<p>Is there an existing solution to create a regular expressions dynamically out of a given date-time format pattern? The supported date-time format pattern does not matter (Joda <code>DateTimeFormat</code>, <code>java.text.SimpleDateTimeFormat</code> or others).</p>
<p>As a specific example, for a given date-time format like <code>dd/MM/yyyy hh:mm</code>, it should generate the corresponding regular expression to match the date-times within the specified formats.</p>

[{'answer_id': 52149, 'author': 'Jason Cohen', 'author_id': 4926, 'author_profile': 'https://Stackoverflow.com/users/4926', 'pm_score': 0, 'selected': False, 'text': '<p><code>SimpleDateFormat</code> already does this with the <code>parse()</code> method.</p>\n\n<p>If you need to parse multiple dates from a single string, start with a regex (even if it matches too leniently), and use <code>parse()</code> on all the potential matches found by the regex.</p>\n'}, {'answer_id': 179044, 'author': 'Keng', 'author_id': 730, 'author_profile': 'https://Stackoverflow.com/users/730', 'pm_score': 1, 'selected': False, 'text': '<p>If you are looking for basic date checking, this code matches this data.</p>\n<pre><code>\\b(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)?[0-9]{2}\\b\n\n10/07/2008  \n10.07.2008\n1-01/2008\n10/07/08    \n10.07.2008\n1-01/08\n</code></pre>\n<p><em>Code via <a href="https://www.regexbuddy.com" rel="nofollow noreferrer">regexbuddy</a></em></p>\n'}, {'answer_id': 181870, 'author': 'Tomalak', 'author_id': 18771, 'author_profile': 'https://Stackoverflow.com/users/18771', 'pm_score': 3, 'selected': True, 'text': '<p>I guess you have a limited alphabet that your time formats can be constructed of. That means, <code>"HH"</code> would always be "hours" on the 24-hour clock, <code>"dd"</code> always the day with leading zero, and so on.</p>\n\n<p>Because of the sequential nature of a time format, you could try to tokenize a format string of <code>"dd/mm/yyyy HH:nn"</code> into an array <code>["dd", "/", "mm", "/", "yyyy", " ", "HH", ":", "nn"]</code>. Then go ahead and form a pattern string from that array by replacing <code>"HH"</code> with <code>"([01][0-9]|2[0-3])"</code> and so on. Preconstruct these pattern atoms into a lookup table/array. All parts of your array that are not in the lookup table are literals. Escape them to according regex rules and append them to you pattern string.</p>\n\n<hr>\n\n<p>EDIT: As a side effect for a regex based solution, when you put all regex "atoms" of your lookup table into parens and keep track of their order in a given format string, you would be able to use sub-matches to extract the required components from a match and feed them into a CreateDate function, thus skipping the ParseDate part altogether.</p>\n'}, {'answer_id': 41363354, 'author': 'Ankit Arjaria', 'author_id': 1779787, 'author_profile': 'https://Stackoverflow.com/users/1779787', 'pm_score': 0, 'selected': False, 'text': '<p>The below given js / jQuery code is for dynamically generated RegEx for the Date format only, not for DateTime (Development version not fully tested yet.)</p>\n\n<p>Date Format should be in "<em>D M Y</em>".</p>\n\n<p>E.g.</p>\n\n<blockquote>\n  <ul>\n  <li>DD-MM-YY</li>\n  <li>DD-MM-YYYY</li>\n  <li>YYYY-MM-DD</li>\n  <li>YYYY-DD-MM</li>\n  <li>MM-DD-YYYY</li>\n  <li>MM-DD-YY</li>\n  <li>DD/MM/YY</li>\n  <li>DD/MM/YYYY</li>\n  <li>YYYY/MM/DD</li>\n  <li>YYYY/DD/MM</li>\n  <li>MM/DD/YYYY</li>\n  <li>MM/DD/YY</li>\n  </ul>\n</blockquote>\n\n<p>Or other formats but created with  "<em>D M Y</em>" characters:</p>\n\n<pre><code>var dateFormat = "DD-MM-YYYY";\nvar order = [];\n    var position = {"D":dateFormat.search(\'D\'),"M":dateFormat.search(\'M\'),"Y":dateFormat.search(\'Y\')};\n    var count = {"D":dateFormat.split("D").length - 1,"M":dateFormat.split("M").length - 1,"Y":dateFormat.split("Y").length - 1};\n    var seprator =\'\';\n    for(var i=0; i&lt;dateFormat.length; i++){\n  if(["Y","M","D"].indexOf(dateFormat.charAt(i))&lt;0){\n    seprator = dateFormat.charAt(i);\n  }else{\n    if(order.indexOf(dateFormat.charAt(i)) &lt;0 ){\n        order.push(dateFormat.charAt(i));\n    }\n  }\n    }\n    var regEx  = "^";\n    $(order).each(function(ok,ov){\n    regEx  += \'(\\d{\'+count[ov]+\'})\'+seprator;\n    });\n    regEx = regEx.substr(0,(regEx.length)-1);\n  regEx  +="$";\n  var re = new RegExp(regEx);\n  console.log(re);\n</code></pre>\n\n<p>NOTE: There is no validation check for months / days\ne.g. month should be in 01-12 or date should be in 01-31\n<br/></p>\n'}]
['https://Stackoverflow.com/questions/52066', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3993/']