26037

Javascript RegEx: validate date time

I am not very good with regex. I am being frustrated by a regular expression.

Example:

I have the following date time: 05/16/2013 12:00 am

I need a javascript regex that respect this format: mm/dd/yyyy hh:mm [am/pm]

var dateReg = /^[0,1]?\d{1}\/(([0-2]?\d{1})|([3][0,1]{1}))\/(([1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))$/; if(!dateReg.test(inputVal)) { $(this).after('<span class="error error-keyup-5">Invalid date format.</span>'); }

But this code works only for date, not with time. Thanks for your help.

Answer1:

Your code snippet does only match the date, there are no atoms to match the time. Your regexp should be:

dateReg = /^[0,1]?\d\/(([0-2]?\d)|([3][01]))\/((199\d)|([2-9]\d{3}))\s[0-2]?[0-9]:[0-5][0-9] (am|pm)?$/

Answer2:

A regexp for am/pm time can be found here:

/^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/

Answer3:

You should not be parsing dates and times yourself. Find a good library to do it. d3.js contains fine support for parsing dates and times according to formats you specify. See https://github.com/mbostock/d3/wiki/Time-Formatting.

Recommend

  • How to automatically change values of combobox value based on input value in text box using php and
  • XPages - get value of a textarea inside a dialog - Part 2
  • Regex for testing date formatting
  • How to test for a propety of a class after promise resolution with mocha and chai
  • How do I start a tone and let it run indefinitely till i stop it explicitly?
  • jquery table row selector based on user input
  • Loading dynamic div content with jQuery from a URL
  • BFS traversal of directed graph from a given node
  • jQuery dynamic image loading
  • Preventing ctrl+z in browser
  • Control key + click event on google map
  • Parsing Java String to date
  • Firebase Save Notification to DB not working when app is not running
  • how to find the index position of the ARRAY Where NSPredicate pick the value. I use filteredArrayUsi
  • Round brackets not showing up correctly in RightToLeft flow direction in WPF
  • Formatting a TimeSpan to look like a time zone offset
  • RavenDB OrderByDescending and Take - Incorrect Results
  • How to retrieve Business Component rows in SIebel Open UI PM?
  • jQuery - how to validate a date of birth using jQuery Validation plugin?
  • A simple datepicker in VueJS
  • How to parsing NSDate to RFC 822 always use in English?
  • stop execution in Custom validator if it false
  • Change device language on Android 6.0 (Android M)
  • Calculate time difference in hh:mm:ss with simple javascript/jquery
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • JSON with duplicate key names losing information when parsed
  • Jquery - Jquery Wysiwyg return html as a string
  • How to delete a row from a dynamic generate table using jquery?
  • using HTMLImports.whenReady not working in chrome
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Java static initializers and reflection
  • Linking SubReports Without LinkChild/LinkMaster
  • Authorize attributes not working in MVC 4
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app