Regex Persian Date validation
Asked Answered
D

8

12

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?

Duenna answered 21/8, 2017 at 5:54 Comment(0)
R
20

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

Remington answered 23/10, 2017 at 14:26 Comment(0)
G
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.

Garrity answered 13/12, 2018 at 10:30 Comment(0)
D
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
Duenna answered 21/8, 2017 at 5:54 Comment(4)
how nice, you ask and answer on same time.Omission
@ecko, I use Q&A-style of stackoverflow stackoverflow.blog/2011/07/01/…Duenna
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.Insecurity
@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.Duenna
T
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

Totally answered 13/6, 2021 at 7:10 Comment(0)
F
0

I edit @MortezaAsadi 's answer to this:

^(\d{4})\/(0?[1-9]|1[012])\/(0?[1-9]|[12][0-9]|3[01])$
Fructose answered 24/1, 2018 at 12:31 Comment(0)
T
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

Thickness answered 5/9, 2021 at 10:46 Comment(0)
T
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

Togetherness answered 31/10, 2022 at 11:16 Comment(0)
E
0

This is a simple regex for validating the Jalaali date.

The year starts with 13 or 14.

^1[3-4]\d{2}((0[1-9])|(1[0-2]))(([0-2][0-9])|(3[0-1]))$
Errhine answered 1/1 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.