I'm trying to get my date range feature to be working correctly. Currently, when I try to select a date it is able to pick the date however when I click apply, it becomes an invalid date.
My code is the following:
AngularJS:
$scope.SimplePickerChange = function () {
$scope.date = {
startDate: $filter("date")(new Date(), 'yyyy-MM-dd'),
endDate: $filter("date")(new Date(), 'yyyy-MM-dd'),
};
};
HTML:
<input date-range-picker
id="date"
name="date"
class="form-control date-picker"
type="text"
ng-model="date"
ng-change="SimplePickerChange();"/>
Why are you changing model value by using filter? If you want to show start & end date in input field in yyyy-MM-dd format then just pass it to configuration options.
If you're using angular-daterangepicker then you can have code like below:
<input date-range-picker class="form-control date-picker" type="text" ng-model="date"
options="options" />
Where options is:
$scope.options = {
applyClass: 'btn-green',
locale: {
applyLabel: "Apply",
fromLabel: "From",
format: "YYYY-MM-DD", //will give you 2017-01-06
//format: "D-MMM-YY", //will give you 6-Jan-17
//format: "D-MMMM-YY", //will give you 6-January-17
toLabel: "To",
cancelLabel: 'Cancel',
customRangeLabel: 'Custom range'
}
}
So, just don't use ng-change function to update model value of the daterangepicker input field. If you really want to do that to post value to some web api then do it separately on some other variable, & not on model var of daterangepicker.
If you really want to set date variable value on load (initially) then start date & end date keep as date/moment objects & not the string (which date filter returns). So, it can be:
$scope.date = {
startDate: new Date(),
endDate: new Date()
};
Official Docs
Update: Plunker Example
Related
all I want to filter the items like (Start and End Date) which are based on invoice_date using the date range functionality in meanjs app.
My problem is the date filter function are working perfectly in plunker and my localHost production but while pushing to server it's showing only up to May month data's if some invoice_date values date has been `2017-08-24 and 2017-07-27' these data is not displaying in table, I don't know where I did the mistake and what I have missed it ..... My Plunk
Please look at my plunker to reference.
I Have displaying invoice_date, so this is the field I want to use for filtering.
So what I exactly looking for, I want to filter the invoice_date as start date and end date : for example:- if we select start date like 24-05-2017 and end date is 24-08-2017 in table this two transaction only need to display or filter... so I have used date range filter to achieve this solution, but in server it's not working for us please help.
In my server if I select end date as today's date all data's are showing perfectly on the table, so I think the problem is based on these fields $scope.from = new Date(2014, 04, 30);
$scope.to = new Date(2019, 08, 25);
So if We set end date as a current or today's date in default, I think
the problem would be solved, so how to set today's date as a default to end date...
Controller:
.filter('dateRange', function() {
return function(records, dateKey, from, to) {
return records.filter(function(record) {
return !moment(record[dateKey], 'YYYY-MM-DD').isBefore(moment(from))
&& !moment(record[dateKey], 'YYYY-MM-DD').isAfter(moment(to));
});
};
})
Html:
<input type="date" class="form-control" name="from" ng-model="from">
<input type="date" class="form-control" name="to" ng-model="to" ng-bind="getDatetime | date:'yyyy-MM-dd'">
Filter:-
ng-repeat="data in record | dateRange : 'invoice_date' : from : to"
I tried the ng-bind method to set default today's date to end date like following:-
In controller:-
$scope.getDatetime = new Date();
In Html:-
<lable>End Date</lable><input type="date" class="form-control" name="to" ng-bind="getDatetime | date:'yyyy-MM-dd'" ng-model="to">
I have created plunker for reference:- My plunker
Showing like the moment is not defined:-
Hi I've got follow code:
angular.module("myApp", []).controller("myController", function($scope) {
$scope.currentOption;
$scope.setCurrentTimespan = function() {
//CODE HERE
};
$scope.timespanList = [{
id: 1,
name: 'morning',
startDate: '19.09.2016 06:00',
endDate: '19.09.2016 11:59'
}, {
id: 2,
name: 'noon',
startDate: '19.09.2016 12:00',
endDate: '19.09.2016 13:29'
}, {
id: 3,
name: 'afternoon',
startDate: '19.09.2016 13:30',
endDate: '19.09.2016 18:29'
}, {
id: 4,
name: 'evening',
startDate: '19.09.2016 18:30',
endDate: '19.09.2016 23:59'
}, {
id: 5,
name: 'night',
startDate: '20.09.2016 00:00',
endDate: '20.09.2016 05:59'
}];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
<select ng-options="option as option.name for option in timespanList" ng-model="currentOption"></select>
</div>
I've got from the backend a object with items for my select. In this list there are some timespans like morning, noon, afternoon etc. This timespans have a startDate and an endDate, which also comes from the backend. For example, the timespan "morning" has follow startDate/endDate: [todays date] 06:00 / [todays date] 11:59. So what I want to do is, when I load the list and fill the select with ng-options, I would like to select the item from the list, which matches with the current timestamp. So I have to get the current date and time for example: 19.09.2016 09:45 and than search in the list the item which is defined for this timestamp and select it in the list. So I have to check the startDate/endDate with the current date / time. This should happen when the list was loaded.
So the result for my local time (19.09.2016 09:45) at this moment would be morning.
Has someone an idea how to do this? I didn't find any answers which can help me...Thanks
EDIT WITH SOLUTION:
I've found a perfect way to solve this problem: I found moment.js, which solves my requirements. After including it into my web app I start to use the queries and functions from moment.js to solve my problem. It works like this:
Get current timestamp (day, month, year, hour and minutes):
let currentTimestamp = moment(); //for example 19.09.2016 11:30
Than I loop throught my array with the timespans and parse the startDate/endDate with the moment.js to my required format DD.MM.YYYY HH:mm and then I can use the moment.js query isBetween() to check, if my currentTimestamp is between the startDate/endDate of each item like this:
this.timespanList.forEach(function(item) {
let startDate = moment(item.startDate, 'DD.MM.YYYY HH:mm'); // for example 19.09.2016 06:00
let endDate= moment(item.endDate, 'DD.MM.YYYY HH:mm'); // for example 19.09.2016 11:59
if(moment(currentTimestamp).isBetween(startDate, endDate)) {
$scope.currentOption = item;
}
});
So if the condition of the looped item is true, I can set the right option in my list. Here are some links of moment.js which describe, how to use it correcty - it's awesome!
moment.js docs: http://momentjs.com/docs/
moment.js parsing: http://momentjs.com/docs/#/parsing/
moment.js queries: http://momentjs.com/docs/#/query/
I hope this is usefull! An alternative solution from the answers which also would work is marked as correct. Thanks and cheers.
You can implement a custom filter to achieve this.
<select ng-options="option as option.name for option in timespanList | momentFilter" ng-model="currentOption"></select>
.filter('momentFilter',function(){
return function (object) {
var array = [];
angular.forEach(object, function (time) {
if(time.startDate.split(" ")[1] == '06:00' && time.endDate.split(" ")[1] == '11:59'){
array.push(time);
}
});
return array;
};
});
I have created a working plunker here.
Do little more work around to achieve this.
EDIT WITH SOLUTION FROM THE QUESTION:
I put my own solution which is in my edited question here so everyone can see it faster:
I've found a perfect way to solve this problem: I found moment.js, which solves my requirements. After including it into my web app I start to use the queries and functions from moment.js to solve my problem. It works like this:
Get current timestamp (day, month, year, hour and minutes):
let currentTimestamp = moment(); //for example 19.09.2016 11:30
Than I loop throught my array with the timespans and parse the startDate/endDate with the moment.js to my required format DD.MM.YYYY HH:mm and then I can use the moment.js query isBetween() to check, if my currentTimestamp is between the startDate/endDate of each item like this:
this.timespanList.forEach(function(item) {
let startDate = moment(item.startDate, 'DD.MM.YYYY HH:mm'); // for example 19.09.2016 06:00
let endDate= moment(item.endDate, 'DD.MM.YYYY HH:mm'); // for example 19.09.2016 11:59
if(moment(currentTimestamp).isBetween(startDate, endDate)) {
$scope.currentOption = item;
}
});
So if the condition of the looped item is true, I can set the right option in my list. Here are some links of moment.js which describe, how to use it correcty - it's awesome!
moment.js docs: http://momentjs.com/docs/
moment.js parsing: http://momentjs.com/docs/#/parsing/
moment.js queries: http://momentjs.com/docs/#/query/
I hope this is usefull! An alternative solution from the answers which also would work is marked as correct. Thanks and cheers.
Hi all I want to filtering the items like (Start and End Date) which is based on Due_date using the daterange functionality in meanjs app. then I tried many ways but unable to get the solution if any one knows the solution please help me..... My Plunk
Please look at my plunker to reference.
I Have displaying Due_date, so this is the field I want to use for filtering.
I have used some functionality to add invoice_Date and terms, which provides the answer like Due_date. for exmple:- invoice_date : 2016-09-10, terms : 6, the answer I got Due_date : 16-09-2016
so what I excatly looking for , I want to filter the Due_date as start date and end date : for example:- if we select start date like 16-09-2016 and end date is 25-09-2016 in table these two transaction only need to display or filter... so I have used daterange filter to achieve this solution, but unable to get the solution please help us.
the daterange filter is working perfectly if we using ng_module is invoice_date, but we don't know how to filter the Due_date filed please help us.... My Plunker
Controller:
.filter('dateRange', function() {
return function(records, dateKey, from, to) {
return records.filter(function(record) {
return !moment(record[dateKey], 'YYYY-MM-DD').isBefore(moment(from))
&& !moment(record[dateKey], 'YYYY-MM-DD').isAfter(moment(to));
});
}
})
Html:
<input type="date" class="form-control" name="from" ng-model="from">
<input type="date" class="form-control" name="to" ng-model="to">
Filter:-
ng-repeat="data in record | dateRange : 'invoice_date' : from : to"
This below the filed need to filter in table:-
Due_date:-
<td> {{addDays(data.invoice_date,data.terms) | date:'dd-MM-yyyy'}}</td>
I have created plunker for referrence:- My plunker
You can create a custom filter for this
HTML
<tr ng-repeat="data in record | myfilter:from:to">
<td> {{data.supplier_name}}</td>
<td> {{data.invoice_date}}</td>
<td> {{data.terms}}</td>
<td> {{addDays(data.invoice_date,data.terms) | date:'yyyy-MM-dd'}}</td>
</tr>
JS
app.filter("myfilter", function() {
return function(items, from, to) {
var df = from;
var dt =to;
var result = [];
for (var i=0; i<items.length; i++){
var date = new Date(items[i].invoice_date);
date.setDate(date.getDate() + parseInt(items[i].terms));
var tf = date;
if (tf > df && tf < dt) {
result.push(items[i]);
}
}
return result;
};
});
There are several examples of HTML5 form options on this page, including the "time" element. Is it possible to force the time element to include a millisecond component?
I'm not concerned for the fallback option where a plain text box is used.
This works:
<input type="time" step="0.001"></input>
Live preview: http://jsbin.com/giqikelumu/edit?html,output
Simply use the step attribute. In case of a input type="time". The step attribute defaults to 60 (1 means 1 second). But you can also set fractions.
<input type="time" step="any" />
As its an input tag, the value can be entered into it by the user then using the step attribute as stated above will surely help.
What if this input is in the form and value can come from some API cal and is given to the form to show it. It can be changed too. If the requirement then is to show or not show the second or millisecond part we can do the following.
When second and millisecond is required
getFormatDate = function (val) { // assuming val is date like "/Date(946673340000)/"
if (val != undefined) {
date = new Date(val.match(/\d+/)[0] * 1); // creating a date object from val
return new Date(date.getFullYear(), date.getMonth(), date.getDate(),
date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
}
}
When second and millisecond is NOT required
getFormatDate = function (val) { // assuming val is date like "/Date(946673340000)/"
if (val != undefined) {
date = new Date(val.match(/\d+/)[0] * 1); // creating a date object from val
return new Date(date.getFullYear(), date.getMonth(), date.getDate(),
date.getHours(), date.getMinutes());
}
}
Is it possible to disable dates when I use
I want to disable current date for one scenario and future dates for other scenario.
How should I disable the dates?
You can add a min or max attribute to the input type=date. The date must be in ISO format (yyyy-mm-dd). This is supported in many mobile browsers and current versions of Chrome, although users can manually enter an invalid date without using the datepicker.
<input name="somedate" type="date" min="2013-12-25">
The min and max attributes must be a full date; there's no way to specify "today" or "+0". To do that, you'll need to use JavaScript or a server-side language:
var today = new Date().toISOString().split('T')[0];
document.getElementsByName("somedate")[0].setAttribute('min', today);
http://jsfiddle.net/mblase75/kz7d2/
Ruling out only today, while allowing past or future dates, is not an option with here. However, if you meant you want tomorrow to be the min date (blanking out today and all past dates), see this question to increment today by one day.
As in all other cases involving HTML forms, you should always validate the field server-side regardless of how you constrain it client-side.
In pure HTML, the only restrictions you can put on dates are its lower and upper bounds through the min and max attributes. In the example below, only the dates of the week I'm posting this question are allowed, other appear greyed out and clicking on them doesn't update the input value:
<input type="date" min="2019-06-02" max="2019-06-08"/>
You can also disable any invalid date by using a few lines of JavaScript, but this doesn't ship with all the native <input type="date"> features like greyed-out dates. What you can do is set the date value to '' in case of an invalid date, an error message could also be displayed. Here is an example of an input that doesn't accept weekend dates:
// Everything except weekend days
const validate = dateString => {
const day = (new Date(dateString)).getDay();
if (day==0 || day==6) {
return false;
}
return true;
}
// Sets the value to '' in case of an invalid date
document.querySelector('input').onchange = evt => {
if (!validate(evt.target.value)) {
evt.target.value = '';
}
}
<input type="date"/>
HTML datepicker (<input type=date>) supports min/max attribute, but it is not widely supported.
At the meantime you may consider using bootstrap-datepicker, v1.2.0 is on github.
References:
W3C spec
You could use this to disable future dates :
Inside you document.ready function, place
//Display Only Date till today //
var dtToday = new Date();
var month = dtToday.getMonth() + 1; // getMonth() is zero-based
var day = dtToday.getDate();
var year = dtToday.getFullYear();
if(month < 10)
month = '0' + month.toString();
if(day < 10)
day = '0' + day.toString();
var maxDate = year + '-' + month + '-' + day;
$('#dateID').attr('max', maxDate);
and in form
<input id="dateID" type="date"/>
Here is the working jFiddle Demo
For react and similar libraries, you may use this to disable all dates before today.
<input type='date' min={new Date().toISOString().split('T')[0]} >
Depending on what you need, you can also use the step attribute to only enable specific dates - e.g. every Monday, or every other day. You can use it in combination with min and max
e.g. every Monday
<input type="date" step="7" value="2022-04-04">
Every Thursday
<input type="date" step="7" value="2022-04-07">
Every other day
<input type="date" step="2">