Date validation using angular js - angularjs

I have start date and end date picker in format of mm-dd-yyyy.End date should be with in 10 days range of start date and end date should not be before start date. How to validate using angular js.Any help is greatly appreciated

I advise you to use moment.js. Here's a basic tutorial and help to get started:
https://dzone.com/articles/getting-started-with-momentjs-intro-with-examples
Basically you would do:
function testDate(startDate, endDate){
var start = moment(startDate, 'mm-dd-yyyy');
var end = moment(endDate, 'mm-dd-yyyy');
if(endDate.isBefore(start)){
//start before end
}
if(startDate.add('days', 11).isAfter(endDate)){
//end not within 10 days range
}
//success!
}
It would be something like that. Hope it helps!

For simple pattern matching, you can use the ngPattern directive. For more complex validation (such as ensuring it's a valid date and within a certain range of another date), you will need to write your own validation directive or use the handy custom validation directive available from Angular-UI.
https://htmlpreview.github.io/?https://github.com/angular-ui/ui-validate/master/demo/index.html

Can you provide more details about this as in what library are you using for datepicker. It would help us answer.
Since you are using a datepicker you can restrict the date range in the fromDate field do not display any dates which are after a certain period (10 days from start date in you case).
jQuery datepicker allows this so does other datepickers.
EDIT:
In your controller do something like this.
var newDate = new Date($scope.startdate.getFullYear(), $scope.startdate.getMonth(), $scope.startdate.getDate()+10);
$scope.dateOptions = {
maxDate: newDate,
dateFormat: 'dd/mm/YYYY'
};
And now you can pass dateOptions to your input.
Note: Post a minimal plunkr for faster resolution of your issues.

Related

how to show date time in datetime picker according to timezone using datetimepicker of anuglarjs indrimuska

I want to set min-date option on datetimepicker and also want to set this option according to the timezone of selected location.
for example :
$scope.minDate = moment().subtract(0, 'month');
but this statement set minDate according to local time instead of selected location timezone. Please, anyone, tell me the solution that how can i set minDate according to the timezone and how to solve this problem because I need the solution
I am using angularjs and its indrimuska plugin to show datetimepicker.
Here is the link of the documentation.
You can use moment timezone for getting a date for your desired timezone. Like in the following example
$scope.minDate = getDateFromTimezone('America/New_York');
function getDateFromTimezone (employeetimeZone) {
return moment().tz( employeetimeZone).format("MM/DD/YYYY");
}

How can I use one daterange picker plugin for two fileds?

I want to use this daterange picker plugin to
http://www.daterangepicker.com/ open for two fields start date and end date. Right now it only opens for one field. Is there a way to achieve it on two fields.
Maybe something like this?
$("#date-range-picker").daterangepicker({}, function(start, end) {
$scope.startDate = start.format('MM/DD/YYYY');
$scope.endDate = end.format('MM/DD/YYYY');
$scope.$apply();
});
See the full fiddle.

Angular interpret my date dd/mm/yyyy as mm/dd/yyyy and changed de day and month

I send a date to my angularjs app in format 22/03/2017 (dd/mm/yyyy) but angularjs interprets it like mm/dd/yyyy. If I print my day, angularjs return my month.
Any idea of how can I will tell Angularjs that my date is in format dd/mm/yyyy?
I solved it! I just convert my string date like this example:
var date = "03/05/2013";
var newdate = date.split("/").reverse().join("-");
"2013-05-03"
It depends where and how are you using your date instance. If it's in template then use angular date filter. If it's in your controller/service then inject angular $filter in your controller/service. You can pass your desired format as input to date filter. Here's the api docs.

Angular date-picker select end of a year

My goal is to make the angular date-picker pick the end of a the selected year and present the 31.12th of this year.
I´m using moment.js.
My code:
scope.ngModel = moment(scope.ngModel).endOf('year');
In my controller it´s:
2013-10-28T10:00:00.000Z
for the time.
When I load the page, it´s "2013-12-31T22:59:59.999Z" -> Perfect!!
But when I´m selecting another year via the date-picker, e.g. 2002, it´s:
"2002-01-01T11:59:59.000Z"
and there´s always the 01.01. of the year.
Any ideas?
Without any refactoring just moment().subtract(1, 'day') one day each time problem occurs or simply after manual picking :D!

Angular JS formate date

Is there any directive in Angular, I want to enable user when he write 2/3/67 in input for date date needs to be formatted like 02/03/1967 I tried few things on my own but they didn't work.
Just add a Angularjs formating, when displaying users input like so
{{yourdate.variable | date:'yyyy-MM-dd'}}
Se more at AnguarJS date function
EDIT
Here is working Plunker, with different type of dates. Enjoy

Resources