Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi team,

I want to use regular expression for Date Format in YYYY/MM/DD.
I am trying this:
/^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$/;

It not work.
its show me an error:
Syntax error in regular expression.

Please kindly help....
Regards
Harshal Raut
Posted

If you want regex for date format like 2013/12/25, try this
(\d){4}(\/)([0][1-9]|[1][012])(\/)([0][1-9]|[12][1-9]|[3][01])

To use this in your code say java, javascript, or c#, you will have to escape the '\' with '\'.
 
Share this answer
 
v2
Comments
[no name] 25-Dec-13 7:31am    
Thank you So much.... Peter Leow.
Wish you a "Merry Christmas"..Enjoy the Christmas..
Peter Leow 25-Dec-13 7:38am    
You are welcome. Merry Christmas and Happy New Year to you too.
Your actual Regex works fine, if I feed it into Expresso it even works - the problem is the string delimiters. You have delimited the string with '/' but your string also contains '/' characters, which will act as the end of the string.... If you use a different string delimiter (double quotes for example) it should work fine.
 
Share this answer
 
Comments
[no name] 25-Dec-13 4:30am    
How it is ?
OriginalGriff 25-Dec-13 4:57am    
I'm fine, thank you for asking.
Or did you mean something else?
[no name] 25-Dec-13 6:34am    
/^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$/;

where should i make the changes?.kindly help.it should be in Date Format in YYYY/MM/DD(2012/10/25).
OriginalGriff 25-Dec-13 7:23am    
Change the outer delimiters:
'^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$';

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900