Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for date format- dd-mm-yyyy in Javascript

Tags:

javascript

I need a regular expression for date format: dd-mm-yyyy in Javascript.

like image 409
Gopesh Avatar asked Jan 20 '12 06:01

Gopesh


People also ask

What is regular expression for date format dd mm yyyy?

For dd-mm-yyyy format, use ^(0[1-9]|[12][0-9]|3[01])[- /.] (0[1-9]|1[012])[- /.] (19|20)\d\d$. You can find additional variations of these regexes in RegexBuddy's library.

What date format is dd mm yyyy in JavaScript?

To format a date as dd/mm/yyyy:Use the getDate() , getMonth() and getFullYear() methods to get the day, month and year of the date. Add a leading zero to the day and month digits if the value is less than 10 .

What is the regular expression for date format?

The regex matches on a date with the YYYY/MM/DD format and a "Date of birth:" or "Birthday:" prefix (Year min: 1900, Year max: 2020). For example: Date of birth: 1900/12/01. Date of birth: 2019.01.

How convert dd mm yyyy string to date in JavaScript?

To convert a dd/mm/yyyy string to a date:Split the string on each forward slash to get the day, month and year. Pass the year, month minus 1 and the day to the Date() constructor. The Date() constructor creates and returns a new Date object.


1 Answers

Here is Regex for multiple date formats working for me :

        //dd.MM.yyyy
        var date_regex = /^(0[1-9]|1\d|2\d|3[01])\.(0[1-9]|1[0-2])\.(19|20)\d{2}$/;
        alert(date_regex.test("02.02.1991"));  

//      //dd/mm/yyyy
//      var date_regex = /^(0[1-9]|1\d|2\d|3[01])\/(0[1-9]|1[0-2])\/(19|20)\d{2}$/;
//          alert(date_regex.test("02/12/1991"));  

//      //dd-mm-yyyy
//      var date_regex = /^(0[1-9]|1\d|2\d|3[01])\-(0[1-9]|1[0-2])\-(19|20)\d{2}$/;
//      alert(date_regex.test("02-12-1991")); 

//      //mm/dd/yyyy
//      var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/;
//      alert(date_regex.test("12/02/1991")); 


//      //yyyy.MM.dd
//      var date_regex = /^((19|20)\d{2})\.(0[1-9]|1[0-2])\.(0[1-9]|1\d|2\d|3[01])$/;
//      alert(date_regex.test("1991.12.02")); 

//      //yyyy/MM/dd
//      var date_regex = /^((19|20)\d{2})\/(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])$/;
//      alert(date_regex.test("1991/12/02")); 

//      //yyyy-MM-dd
//      var date_regex = /^((19|20)\d{2})\-(0[1-9]|1[0-2])\-(0[1-9]|1\d|2\d|3[01])$/;
//      alert(date_regex.test("1991-12-02"));
like image 51
Ninad Pingale Avatar answered Oct 12 '22 04:10

Ninad Pingale