Jump to content

Recommended Posts

12.10.2012 format

Parsing year -  ^[0-9]{2}\.[0-9]{2}\.([0-9]{4})
Parsing Month -    \.([0-9]{2})\.
parsing day -	  ^([0-9]{2})\.

 

 

2012-11-10

 

Parsing year    ^([0-9]{4})\.
Parsing month    \.([0-9]{2})\.
Parsing day	 ^[0-9]{4}\.[0-9]{2}\.([0-9]{2})

 

We have a field on the page, where the page should support both the expression.

or condition should be for the year

^[0-9]{2}\.[0-9]{2}\.([0-9]{4})|^([0-9]{4})\.  

which doesn't work.

Link to comment
https://forums.phpfreaks.com/topic/271627-date-format/#findComment-1397688
Share on other sites

Let's translate the RegExps for you, so that you can see where you went wrong. ;)

 

^        # Bind to the start of the string.
[0-9]{2}    # Match two Digits (0-9).
\.        # Followed by a literal period.
[0-9]{2}    # Then another two digits.
\.        # And another literal period.
(        # Open a capturing sub group, which is saved as group 1
 [0-9]{4}    # Then match four digits.
)        # Close the sub group.

 

So, as you can see you're matching a whole lot more than just the year here, and the same with the "day" RegExp in the second format. What I'd do, is to take the two patterns highlighted, as they match the entire date format, and scrap the rest. Then I'd merge them together, and possibly move the binding caret out. Like this:

/^(?:\d{2}\.\d{2}\.\d{4}|\d{4}\.\d{2}\.\d{2})\z/

 

That'll match either of the two date formats, and only that. If there's anything else in the string it'll fail. If you want to grab a certain subset of the date, then add a single pair of parenthesis around the section on both sides of the pipe. Note that doing it will cause you to get the result in two groups. Group #1 will be from the first format, and group #2 from the second. Only one of these will contain a value at any given time.

Edited by Christian F.
Link to comment
https://forums.phpfreaks.com/topic/271627-date-format/#findComment-1397699
Share on other sites

We have a field on the page

 

Here's a slightly different slant on the problem. You should not allow visitors to type in entire dates, even if you display the expected format(s) right next to where they are entering the dates, because you will end up with some visitors that originally came from different locations or that don't/didn't read the expected format(s), entering dates with the months/days transposed.

 

You should use three separate drop-down select menus and/or a javascript pop-up datepicker that would submit the year, month, and day in one known format.

Link to comment
https://forums.phpfreaks.com/topic/271627-date-format/#findComment-1397700
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.