How to show month and year only in 720kb AngularJS Datepicker - angularjs

I'm using this angularJS datepicker in my application and would like to know if there is a way to display only months and years in the calendar and remove the days?
This is how I'm using the datepicker:
<datepicker date-format="MM yy" button-prev-title="previous month" button-next-title="next month" button-prev="<i class='fa fa-arrow-left'></i>" button-next="<i class='fa fa-arrow-right'></i>" id="dtDate" name="dtDate" >
<input ng-model="dtDate" />
</datepicker>
I tried to add date-format="MM yy" but it only changes the selected date and not the datepicker itself.

I think you might be interested in using this git repository that I created.
https://github.com/morfsys/multiple-month-picker
This is how you can use it in your project:
HTML
<input type="text" multiple-month-picker />
JS
//Initiate your app by injecting 'mmp-morfsys'
var app = angular.module('app-name', ['mmp-morfsys'])
It helps you to select multiple months across multiple years. The best thing? It is on AngularJS.
I hope it helps you.

I have the same problem with angularjs-datepicker.
I used another datepicker: angularjs-material.
Here is how you can use it:
<md-datepicker ng-model="startDate" md-mode="month" md-placeholder="dd/MM/yyyy" md-date-locale="mylocale" md-open-on-focus></md-datepicker>
And then in your controller:
function pad(n) {return n < 10 ? "0"+n : n;}
$scope.mylocale = {
formatDate: function(date) {
var d=new Date(date);
if(isNaN(d.getTime())) {
return '';
} else {
return pad(d.getDate())+"/"+pad(d.getMonth()+1)+"/"+d.getFullYear();
}
},
parseDate: function(date) {
var ds = date.split('/');
var d=new Date(ds[2],ds[1]-1,ds[0]);
if(isNaN(d.getTime())&&d!=''){
return new Date(NaN);
} else {
return d;
}
} };
And it is working. Just make sure that you properly handle case when input is invalid (form.startDate.$invalid).
If someone has a solution for angularjs-datepicker, please let us know.

Related

lightning:input Date Field validation

I am trying to use Standard Date picker using below component but however I want to display custom error message when the enterted date is not in the speciied min & max range. But I am not able to achieve it as I could see the standard error message. I implemented the same by referring to : https://developer.salesforce.com/docs/component-library/bundle/lightning:input/example and in this link I can see that the custom error messages can be shown. But nothing worked for me can anyone please help.
https://developer.salesforce.com/docs/component-library/bundle/lightning:input/documentation
<lightning:input aura:id="field
type="date"
name="MyDatefield"
label="MyDateField"
value="2017-09-07"
min="2017-09-05"
messageWhenRangeUnderflow="Select correct date range1"
max="2017-09-22"
messageWhenRangeOverflow="Select correct date range2"
onchange="{!c.checkValidity }"
</aura:component>
This was asked 1 month back but sharing the answer if anybody else runs across this. Use lightning's setCustomValidity() and reportValidity() methods.
Component:
<aura:component >
<lightning:input aura:id="field"
type="date"
name="MyDatefield"
label="MyDateField"
value="2017-09-07"
min="2017-09-05"
max="2017-09-22"
onblur ="{!c.checkValidity }"
/>
</aura:component>
Controller:
({
checkValidity : function(component, event, helper) {
var inputCmp = component.find("field");
inputCmp.setCustomValidity(""); //reset error
var value = inputCmp.get("v.value");
console.log('value: '+value);
var lowerRange = new Date("2017/09/05");
var higherRange = new Date("2017/09/22");
if(!inputCmp.checkValidity()){
if(Date.parse(value)){
if (Date.parse(value) < lowerRange) {
inputCmp.setCustomValidity("Select correct date range1");
}else if (Date.parse(value) > higherRange){
inputCmp.setCustomValidity("Select correct date range2");
}
}else{
inputCmp.setCustomValidity("Invalid value");
}
}
inputCmp.reportValidity();
}
})

Change date format for ui datepicker (jHipster)

I've generated a Spring Boot + Angular 1 based project in jHipster. I'm kind of newbie the Angular world. Everything works fine, but when I input the date using the provided UI datepicker (https://angular-ui.github.io/bootstrap), its format is yyyy-MM-dd, even if I've defined spanish to be my only language package (I would prefer it to be dd/MM/yyyy). That's the code snippet for the input:
regulation-version-dialog.html
<div class="form-group">
<label class="control-label"
data-translate="selfprotectionApp.regulationVersion.versionDate"
for="field_versionDate">Version Date</label>
<div class="input-group">
<input id="field_versionDate" type="text" class="form-control"
name="versionDate" uib-datepicker-popup="{{dateformat}}"
ng-model="vm.regulationVersion.versionDate"
is-open="vm.datePickerOpenStatus.versionDate" /> <span
class="input-group-btn">
<button type="button" class="btn btn-default"
ng-click="vm.openCalendar('versionDate')">
<i class="glyphicon glyphicon-calendar"></i>
</button>
</span>
</div>
</div>
Here there's the {{dateformat}} binding, but I don't know where jHipster picks it from. The only matches are in the date-util.service.js file, but changing the format here doesn't seem to affect.
date-util.service.js
(function() {
'use strict';
angular
.module('selfprotectionApp')
.factory('DateUtils', DateUtils);
DateUtils.$inject = ['$filter'];
function DateUtils($filter) {
var service = {
convertDateTimeFromServer: convertDateTimeFromServer,
convertLocalDateFromServer: convertLocalDateFromServer,
convertLocalDateToServer: convertLocalDateToServer,
dateformat: dateformat
};
return service;
function convertDateTimeFromServer(date) {
if (date) {
return new Date(date);
} else {
return null;
}
}
function convertLocalDateFromServer(date) {
if (date) {
var dateString = date.split('-');
return new Date(dateString[0], dateString[1] - 1, dateString[2]);
}
return null;
}
function convertLocalDateToServer(date) {
if (date) {
return $filter('date')(date, 'yyyy-MM-dd');
} else {
return null;
}
}
function dateformat() {
return 'yyyy-MM-dd';
}
}
})();
Of course, I could replace the {{dateformat}} expression by some date format in HTML, but I would like to do it for the whole project.
There is no default way in angular JS to set up dateformat from a provider for example for the complete project.
What you could do; is define a filter with your format to be used like that : {{dateformat | date:'MM/dd/yyyy'}}
OR define a global filter for that requirement to make it more easy to type (source https://stackoverflow.com/a/18402623/3687474 )
.filter('myDate', function($filter) {
var angularDateFilter = $filter('date');
return function(theDate) {
return angularDateFilter(theDate, 'dd MMMM # HH:mm:ss');
}
});
and used it like that : {{dateformat | myDate }}
The ultimate solution is to used a very robust library to manage your dates such as moment.js and it angularjs diretives
this is my favorite solution; as it is more scalable for many uses and date manipulation.
However; adding libraries to your project has always a performance cost ;)

how to disable dates in kendo date time picker (Angular)

<input id="startDate" kendo-date-time-picker
k-ng-model="vm.startDate"
k-on-change="vm.updateStartDate()"
required
/>
How to add disabled dates to this date picker?
Not using jquery !!
Is there a attribute as k-disabled-dates?
Specify the k-options attribute:
<input id="startDate" kendo-date-time-picker
k-ng-model="vm.startDate"
k-on-change="vm.updateStartDate()"
required,
k-options="startDateOptions"
/>
And then implement the options with the disabledDates(http://docs.telerik.com/kendo-ui/api/javascript/ui/datetimepicker#configuration-disableDates) config specified however is appropriate for your situation, i.e.
$scope.startDateOptions = {
disableDates: function (date) {
var disabled = [13,14,20,21];
if (date && disabled.indexOf(date.getDate()) > -1 ) {
return true;
} else {
return false;
}
}
};
Example: http://dojo.telerik.com/#Stephen/eLuWE

Angularjs datepicker - how to set mutually dependent dates

I am working on a feature that requires me to inactivate an entity for a given period. So, I show two date pickers : 1) Starting date and 2) Ending date.
The two dates are interdependent in a manner where the starting date limits the min date for ending date and the ending date limits the max date for starting date.
<input type="text" ui-date="inactive.datePicker" ui-date-format="mm/dd/yy" ng-model="inactive.details.fromDate" id="fromDate" ng-change="error.dueDate=false" value="inactive.details.fromDate}}"/>
<img ng-click="showFromDatePicker($event)" class="ui-datepicker-trigger" src="../design/calendar3.gif" >
<input type="text" ui-date="inactive.datePicker" ui-date-ormat="mm/dd/yy" ng-model="inactive.details.toDate" id="toDate" ng-change="error.dueDate=false" value="{{inactive.details.toDate}}"/>
<img ng-click="showToDatePicker($event)" class="ui-datepicker-trigger" src="../design/calendar3.gif" >
$scope.showFromDatePicker = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.inactive.fromDatepicker = {
max : $scope.inactive.details.toDate;
}
angular.element("#fromDate").focus();
};
$scope.showToDatePicker = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.inactive.toDatepicker = {
max : $scope.inactive.details.fromDate;
}
angular.element("#toDate").focus();
};
But right now this piece of snippet isn't exactly working properly. The min date is setting properly but let's kind of disabling the datepicker so I couldn't select a date. Also how do I reinitialize or refresh the datepicker in AngularJS like we could do in Jquery by
$("#myDatepicker").datepicker("refresh");
In angular-ui-bootstrap 0.14.3 I managed to achieve this just by binding min and max to the other model. I use a wrapper directive around datepicker but it shouldn't make any difference:
Begin datepicker: max-date="myobject.endDate"
End datepicker: min-date="myobject.beginDate" max-date="today"

How to compare current time with JSON data

I have JSON data as below:
[
{
"morning":"5:36 am",
"evening":"7:00 pm"
}
]
I want to compare JSON data with current time. Ex: is morning > current time ?
HTML:
<div ng-repeat="item in schedule">
<div>Morning - {{item.morning}}</div>
<div>Eveing - {{item.evening}}</div>
</div>
I have specified the AngularJS part to display the Data and It's working. Now I want to compare the {{item.morning}} with current time and display a message in front of {{item.morning}} as "Time up" based on condition.
Please let me know how can I compare time in AngularJS. Thanks in advance.
As #PWKad mentioned, you should use a date library like moment.js.
One reason for that, might be that trying to implement it yourself can end with an ugly, quick-and-(really)-diry code like this:
Controller:
$scope.isTimeUp = function(time) {
var hour, minute;
if(time.split(" ")[1] == 'pm' && time.split(":")[0] != '12') {
hour = parseInt(time.split(":")[0]) + 12;
} else {
hour = parseInt(time.split(":")[0]);
}
minute = parseInt(time.split(":")[1]);
var today = new Date();
var givenDate = new Date();
givenDate.setHours(hour);
givenDate.setMinutes(minute);
if(givenDate < today) {
return true;
} else {
return false;
}
}
html:
<div ng-repeat="item in schedule">
<div>
Morning - {{item.morning}}
<span ng-if="isTimeUp(item.morning)">
Time's up!
</span>
</div>
<div>
Eveing - {{item.evening}}
<span ng-if="isTimeUp(item.evening)">
Time's up!
</span>
</div>
</div>
...and this is following your markup. You might want to implement a custom filter instead. But again, use moment.js.
Anyways, it seems to be working, though I haven't put much thought in it or tested it too much - see demo.

Resources