Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

problem with regular expression for date formate

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all,

Can any body give me regular expression for date formate
mm/dd/yyyy hh:mm:ss A.M or P.M
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what have you come up with?

Eric
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(?n:^(?=\d)((?<month>(0?[13578])|1[02]|(0?[469]|11)
(?!.31)|0?2(?(.29)(?=.29.((1[6-9]|[2-9]\d)
(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00))
|(?!.3[01])))(?<sep>[-./])
(?<day>0?[1-9]|[12]\d|3[01])
\k<sep>(?<year>(1[6-9]|[2-9]\d)\d{2})
(?(?=\x20\d)\x20|$))?(?<time>((0?[1-9]|1[012])
(:[0-5]\d){0,2}(?i:\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$)


[I added line breaks so the page is not billion characters wide - Eric]
[ January 25, 2008: Message edited by: Eric Pascarello ]
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Persoanlly I would not try to figure out the 30/31/leap year stuff in the regular expression. I would let a second check figure it out. If you HAVE to do it in a regular expression it will be yucky. I will code the whole thing here for fun durning my lunch.

mm/dd/yyyy

space

hh:mm:ss

space

A.M or P.M [I left the . there, I do not know anyone that does that!]


From here I will build a function that tests for a valid date. It will test to make sure that someone does not set 31 days in the wrong month.



With that function, we can combine it with our regular expression and build another function that can do the total valid date/string format



So now we have the function. How do we use this thing?



I just copied all of the code I wrote here over to a test page to see if it works and I got true, false, false, false so it is looking good.

If you need to have the regular expression validate everything without a second check, it will be hard, but if we break it up into parts like I did above it maybe a little easier. Need to figure out patterns for 29/30/31 and for leap years.

Eric
[ January 25, 2008: Message edited by: Eric Pascarello ]
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric is insane.
 
Author
Posts: 974
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Eric Pascarello:
A.M or P.M [I left the . there, I do not know anyone that does that!]



This is mostly off-topic, I know, but some people use dots and some don't. If you're going to use a dot, though, be sure to use two of them (A.M. or P.M.).

I think everyone agrees that a dot after A (or P) but not after the M would be in error.

references: one, two
 
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic