0

It should allow

1/1/17 1:10
10/10/2017 01:10

Not allowed variants:

1/1/17    1:10
10/10/201701:10

what I have

let activityDateRegex = /^(((0[13578]|1[02])[\/](0[1-9]|[12]\d|3[01])[\/]((19|[2-9]\‌​d)\d{2})\s(0[0-9]|1[‌​0-2]):(0[0-9]|[1-59]‌​\d)\s(AM|am|PM|pm))|‌​((0[13456789]|1[012]‌​)[\/](0[1-9]|[12]\d|‌​30)[\/]((19|[2-9]\d)‌​\d{2})\s(0[0-9]|1[0-‌​2]):(0[0-9]|[1-59]\d‌​)\s(AM|am|PM|pm))|((‌​02)[\/](0[1-9]|1\d|2‌​[0-8])[\/]((19|[2-9]‌​\d)\d{2})\s(0[0-9]|1‌​[0-2]):(0[0-9]|[1-59‌​]\d)\s(AM|am|PM|pm))‌​|((02)[\/](29)[\/]((‌​1[6-9]|[2-9]\d)(0[48‌​]|[2468][048]|[13579‌​][26])|((16|[2468][0‌​48]|[3579][26])00))\‌​s(0[0-9]|1[0-2]):(0[‌​0-9]|[1-59]\d)\s(AM|‌​am|PM|pm)))$/;
Jeet
  • 23
  • 6
  • where is your tried code? – Himanshu Tanwar Jan 27 '17 at 05:40
  • ...in addition to code, can you _clearly_ show us the matching and non-matching date patterns? – Tim Biegeleisen Jan 27 '17 at 05:41
  • let activityDateRegex = /^(((0[13578]|1[02])[\/](0[1-9]|[12]\d|3[01])[\/]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((0[13456789]|1[012])[\/](0[1-9]|[12]\d|30)[\/]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((02)[\/](0[1-9]|1\d|2[0-8])[\/]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((02)[\/](29)[\/]((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm)))$/; – Jeet Jan 27 '17 at 05:43
  • 2
    @Abhijeet a small note. When someone asks you to show your effort, you should add your effort in question and not in comment – Rajesh Jan 27 '17 at 06:02
  • Do you understand what this regex means? – Toto Jan 27 '17 at 12:54
  • @Toto this is not duplicate question – Jeet Jan 28 '17 at 08:30
  • In fact it is. Learn first what your regex does and you will be able to change it to meet your requierement. – Toto Jan 28 '17 at 10:33

1 Answers1

-1

This will do the trick \d\d?\/\d\d?\/(\d\d)?\d\d\s\d\d?:\d\d

https://regex101.com/r/yqMk1n/1

Roko C. Buljan
  • 164,703
  • 32
  • 260
  • 278
pixelarbeit
  • 454
  • 2
  • 11
  • thanks Roko C. Buljan, Dennis Koch but it should not accept date more than 31 and monthe more than 12. – Jeet Jan 27 '17 at 07:02
  • @Abhijeet note that the above makes no difference in `dd/mm/yyyy` and `mm/dd/yyyy` ;) https://en.wikipedia.org/wiki/Date_format_by_country – Roko C. Buljan Jan 27 '17 at 07:04
  • Dennis, note also that `\d\d?\/` can match `00/` ... if that's a valid date I'm superman!! – Roko C. Buljan Jan 27 '17 at 07:11
  • Sorry, I was on my mobile phone and tried to provide a quick solution. Before I'm reinventing the whell I suggest this regex: https://regex101.com/library/pZ4gG4 – pixelarbeit Jan 27 '17 at 07:19