• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with regular expression for date formate

 
sirisha makkapati
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
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what have you come up with?

Eric
 
javark rkjava
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
Rancher
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 ]
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric is insane.
 
Brian Cole
Author
Ranch Hand
Posts: 903
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic