ng-pattern for Date in Multiple Formats - angularjs

I am trying to match an input field to be either MM/dd/yyyy, MM-dd-yyyy, or MMddyyyy.
I am a beginner to regular expressions but I wrote the following:
/^[0-9][0-9][\-\/0-9][0-9][0-9][\-\/0-9][0-9][0-9]([0-9][0-9])/
This matches both MM/dd/yyyy and MM-dd-yyyy but not MMddyyyy. It would also match just 10 numbers in a row which I don't want. Help would be greatly appreciated.

Use the following ng-pattern to match the MM/DD/YYYY.Which will also validate the Leap year....
ng-pattern='/^((0[13578]|1[02])[\/.]31[\/.](18|19|20)[0-9]{2})|((01|0[3-9]|1[1-2])[\/.](29|30)[\/.](18|19|20)[0-9]{2})|((0[1-9]|1[0-2])[\/.](0[1-9]|1[0-9]|2[0-8])[\/.](18|19|20)[0-9]{2})|((02)[\/.]29[\/.](((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))$/
Codepen

So this code will work for what I want but it does not check for it being an actual date. For instance February 31st would be accepted.
/^([0-9]{2}[-/][0-9]{2}[-/][0-9]{4})|([0-9]{8})/

Use the following ng-pattern to match the MM/DD/YYYY.
<md-datepicker flex
id="start"
label="start"
name="start"
ng-model="vm.start"
md-placeholder="Start Date"
ng-pattern="^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$"
required>
</md-datepicker>

Using this regular expression in my project I found a little bug with the dates 10/29/YYYY and 10/30/YYYY. With the correction now it's working perfectly for me. The correct expression is:
ng-pattern='/^((0[13578]|1[02])[\/.](29|30|31)[\/.](18|19|20)[0-9]{2})|((01|0[3-9]|1[1-2])[\/.](29|30)[\/.](18|19|20)[0-9]{2})|((0[1-9]|1[0-2])[\/.](0[1-9]|1[0-9]|2[0-8])[\/.](18|19|20)[0-9]{2})|((02)[\/.]29[\/.](((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))$/'>

Related

Angularjs date pipe filter gives wrong output

Currently I am using angularjs in my project in which i am converting a date format '2019-05-09T20:09:11.677Z' using angular js to 'MM/dd/yyyy' but its giving me wrong output '05/10/2019'. Expected Output is '05/09/2019'.
What is the issue? Can anybody help me.
{{'2019-05-09T20:09:11.677Z' | date:'MM/dd/yyyy'}}
Current Output (Wrong): 05/10/2019
Expected Output (Right) : 05/09/2019
Date filter considers your timezone. Since the date you're trying to print has 8pm as time, I guess you're in a timezone in the range +0400 and +1200, making the date falling into 05/10/2019.
If you want to force the timezone to something different, e.g. UTC, you can use
{{ '2019-05-09T20:09:11.677Z' | date:'MM/dd/yyyy' : '+0000' }}
You have to specify timezone as one of parameters.
{{ date_expression | date : format : timezone}}
eg:
{{date| date:'shortDate':'GMT' }}
For more Information AngularJs
Small demo for your help Demo

ng-pattern to validate dd/mm/yy format date in angularjs

i have a date picker which accepts date in dd/mm/yy format can someone please say what is the regular expression should be place in ng-pattern to match the same
date will contain only / no other special character
example:12/12/2015
regex for that would be
ng-pattern="/\d\d/\d\d/\d\d\d\d/"
The regex will be
/^(((0[1-9]|[12]\d|3[01])-(0[13578]|1[02])-((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)-(0[13456789]|1[012])-((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])-02-((19|[2-9]\d)\d{2}))|(29-02-((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/
This is valid for leap years as well. To use this in your application, you can either use it inline with ng-pattern or use it as
$scope.pattern = new RegExp(PatternAsString);
<input ng-model="foo" ng-pattern="pattern">
Use below pattern. It only validates date format "yyyy/MM/dd".
ng-pattern = ^[0-9]{4}[\/][0-9]{2}[\/][0-9]{2}$
The regex is
ng-pattern="/^(31[ \/ ](0[13578]|1[02])[ \/ ](18|19|20)[0-9]{2})|((29|30)[\/](01|0[3-9]|1[1-2])[\/](18|19|20)[0-9]{2})|((0[1-9]|1[0-9]|2[0-8])[\/](0[1-9]|1[0-2])[\/](18|19|20)[0-9]{2})|(29[\/](02)[\/](((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))$/"
This will take care of Leap year as well.
Format is DD/MM/YYYY
This pattern is correct and working fine.
ng-pattern="/^(0[1-9]|[1-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/[0-9]{4}$/"
#ivarni
your condition won't work for 29/oct or 30/oct
your month validation need minor rectification.
What we have: (01|0[3-9]|1[1-2])
Corrected : (01|0[3-9]|1[0-2])

Does parsley.js support type="number" fields that have leading zeros, like zip code?

We have a zip code field. Right now we have it as type="text", but that means that phones don't use the numeric keypad. But before, when we had it as a type="number" the leading zeros would disappear - I think because of Parsley.js. Does this sound right? Is there a parsley.js option that allows us to use the number type without removing leading zeros?
I am using type="tel" plus a pattern.
It's not because of Parsley, it's because of the spec.
Use type=tel and pattern="-?\d+"

Regular Expression date time using MVC Data Annotations

I'm mixing MVC Data Annotations and AngularJs validations ng-pattern.
What I've done so far is this thing:
[RegularExpression("/^[0-9]{4}-[0-9]{2}-[0-9]{2} 20|21|22|23|([0-1][0-9]):[0-5][0-9]:[0-5][0-9]$/", ErrorMessage = "Date format: yyyy-mm-dd hh:mm:ss")]
As you can see, I try to format date: yyyy-mm-dd hh:mm:ss.
I want to make it 24 hours time.
My problem is that form is getting valid when I type:
2015-21 , 2015-22 // 2015-20 is not valid, cannot understand why
2015-12-20 21 // I want user to enter minutes and seconds, because it also has datetimepicker, which is more useful and it sets format as I want
So, why my regular expression is not working as I expect?
Your regex does not work as expected because you did not use a ^ anchor (although I guess this expression is anchored, but still it is better to play it safe) and you did not enclose the alternatives into a group, and thus 21, 22, 23 are valid values.
Here is a fixed expression:
^[0-9]{4}-[0-9]{2}-[0-9]{2} (?:20|21|22|23|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$
^^^ ^^
See demo
change your regex instead to be like this
^[0-9]{4}-[0-9]{2}-[0-9]{2} ((20|21|22|23)|([0-1][0-9])):[0-5][0-9]:[0-5][0-9]$
check this Demo
I only changed 20|21|22|23|([0-1][0-9]) in your regex to ((20|21|22|23)|([0-1][0-9]))

CakePHP: CakeTime i18nFormat for date and time

I'm using the CakeTime class for my localization of dates & times.
For dates it works like I want it to:
$timestring = $this->Time->format('Y-m-d H:i:s', time());
echo 'DateTime: '.$this->Time->i18nFormat($timestring);
// Result => DateTime: 11/08/2013
I want it to also display the time.
For example in the US they use AM/PM and in other places they use the 24 hour notation.
I've looked but can't seem to find a way to do this.
Any idea's?
Edit*
To be clear, the localization works perfectly for the dates(have the LC_TIME files), but the i18nFormat function only returns the date, and from what i saw, passing a format will use that format, not the localized one, example MM/DD/YYYY vs DD.MM.YYYY in a different locale
*Edit2:
The solution vicocamacho gave in the comments is the correct one
So to get the Date + Time in the localized form:
$this->Time->i18nFormat(time(), '%x %X') does the trick!
You can use the TimeHelper::i18nFormat method. You also can check this repo to find common date/time translations https://github.com/cakephp/localized be sure to store them in the APP/locale/<locale>/LC_TIMEdirectory

Resources