11

I want a Regular Expression for validation Persian date like 1396/4/3, 1396/12/08 or something else.
in other words, I want to ensure that format of Persian Date (as String) is some thing like these valid formats are:

1.YYYY/MM/DD

2.YYYY/MM/D

3.YYYY/M/DD

4.YYYY/M/D


any Solution?

Morteza Asadi
  • 1,819
  • 2
  • 22
  • 39

7 Answers7

19

use this Regex :

/^[1-4]\d{3}\/((0[1-6]\/((3[0-1])|([1-2][0-9])|(0[1-9])))|((1[0-2]|(0[7-9]))\/(30|([1-2][0-9])|(0[1-9]))))$/

this regex in jquery full check your persian date template

Year : 4 digits starts with 1300 or 1400

Month,Day : for the 6 first months of a year calculate 31 days and for the 5 next months of a year calculate 30 days and for last month of a year calculate 29 days

Amir H KH
  • 351
  • 2
  • 18
3

This is a bit long but it works everywhere (for example in grep -E in bash):

^[1][1-4][0-9]{2}\/((0[1-6]\/(0[1-9]|[1-2][0-9]|3[0-1]))|(0[7-9]\/(0[1-9]|[1-2][0-9]|30))|(1[0-1]\/(0[1-9]|[1-2][0-9]|30))|(12\/(0[1-9]|[1-2][0-9])))

the format is: YYYY/MM/DD

from year 1100 to 1499

and Esfand is 29 days in this expression.

2

use this regex:

^(\\d{4})/(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])$

with this regex:

  • Year is 4 digits.
  • Month is smaller than equal 12
  • Day is smaller than equal 31
Morteza Asadi
  • 1,819
  • 2
  • 22
  • 39
  • 5
    how nice, you ask and answer on same time. – Masoud Andalibi Aug 21 '17 at 05:55
  • 1
    @ecko, I use `Q&A-style` of stackoverflow http://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/ – Morteza Asadi Aug 21 '17 at 05:57
  • How do you want to validate by regexp if for example the leap day Esfand 30 is valid or not? And some months never have 31 days so your suggested regexp-based answer is not sufficient. – Meno Hochschild Aug 21 '17 at 09:35
  • 1
    @meno-hochschild Regex don't exempt you from coding, a Persian Calendar library may satisfies all requirements that you mentioned, But Regex is a sequence of characters that define a search pattern. Sophisticated thinking makes problem more complex. In other words, this Regex can be a part of a library. – Morteza Asadi Aug 21 '17 at 10:04
1

I recommend using this:

^1[34][0-9][0-9]\/(0?[1-9]|1[012])\/(0?[1-9]|[12][0-9]|3[01])$

It supports:

Year: 1300-1499

Month: 1-12 and 01-12

Day: 1-31 and 01-31

0

I edit @MortezaAsadi 's answer to this:

^(\d{4})\/(0?[1-9]|1[012])\/(0?[1-9]|[12][0-9]|3[01])$
jamshid
  • 225
  • 5
  • 12
0

I use this
^(1[3-4][0-9][0-9])//$

to include 1300/01/01 .. 1499/12/31

and these also give errors

1200/01/01 .. 1400/13/01 .. 1400/12/32

BehrouzMoslem
  • 9,053
  • 3
  • 27
  • 34
0

1[3-4]\d\d\/(1[0-2]|[1-9]|0[1-9])\/(0[1-9]|[1-2][0-9]|3[0-1]|[1-9])($)

year: 1300-1499

month: 1-12 or 01-12

day: 1-31 or 01-31

Ali H
  • 132
  • 8