Date range picker - invalid date - javascript

I am using this date range picke (http://www.daterangepicker.com/#options)
Here are some of options, I understand the most but I need help about "isInvalidDate"
I use this code and it works perfectly. Date 11/12/2015 is disabled and users can't select it.
isInvalidDate: function(date) {
if (date.format('YYYY-MM-DD') == '2015-11-12') {
return true;
} else {
return false;
}
},
But I need to add few dates to invalid, so user can't use them.
I don't know how to do some array and loop through to return true or false days, could anyone help me with this?

I hope it will help someone
var some_date_range = [
'02-04-2016',
'03-04-2016',
'04-04-2016',
'05-04-2016'
];
"isInvalidDate" : function(date){
for(var ii = 0; ii < some_date_range.length; ii++){
if (date.format('DD-MM-YYYY') == some_date_range[ii]){
return true;
}
}
}

You'll need a way to feed the blocked date from your backend to the client.
But let's suppose you solved that and have the dates in an array.
All you need to do then is to check if the date in in the array.
See e.g. here:
Checking if date belongs to array of dates
To get the invalid dates from the backend you could either put them in the script itself, of have the script fetch the to be blocked dates from the server using e.g. ajax.
Don't forget to revalidate on the server, never trust filtering in the clients to actually happen.

Related

Server-Side and Time-Specific script execution

I have a list of dates in an array(for now we can say that the dates are sorted). I want to have a script execute when the date matches a date in the array. My issue is figuring how to make this work on its own. I would like to have a server somehow act like an alarm clock that can run a script for a scheduled date and time. If anyone could help with suggestions to make this work I would appreciate it.
set date >>> if (currentDate == set date) >>> run script for the respective data
Please ask if you need clarification.
The way to do this with parse is a class with a date attribute. Create one object per date in your array of dates (they needn't be sorted).
Create a scheduled job that upon running, query's the class for the first date equal to the current date. If one is found, do whatever you want to do when an alarm is triggered.
So, something like...
Parse.Cloud.job("checkStatus", function(request, status) {
var today = new Date();
today.setHours(0, 0, 0, 0);
var tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
var query = new Parse.Query("MyAlarmClass");
query.greaterThanOrEqualTo('theDateAttribute', today);
query.lessThan('theDateAttribute', tomorrow);
return query.first().then(function(anAlarm) {
if (anAlarm) {
// do whatever should be done on the alarm
} else {
// do nothing
}
}).then(function() {
status.success();
}, function(error) {
status.error(JSON.stringify(error));
});
});
Schedule this to run at least twice per day (or faster than whatever resolution you need on the alarms).

Detect SQL min date string in javascript

In an angularJS application I'm working on, we are using a SQL server to provide data. Nothing odd there. I am tasked with working on the date sanitation, so objects that are passed into our application that have the SQL min date value are undefined, and ones that do have a date are javascript Date objects.
I know that during upgrades, sometimes the string that is defined for min date can change. Right now, it is 0001-01-01T00:00:00Z. I cannot just do a string comparison because it could change.
My problem is, how do i determine this min date in javascript? new Date(varThatIsMinDate) is a valid date. MomentJS thinks its valid too, and it is technically a valid date. I'm less worried about validation and more worried about "is it min date"
How do you determine min date === true from a zulu pattern 0001-01-01T00:00:00Z or similar in javascript?
My code so far, cause I know you are gonna ask for it:
if (response.config.responseType === 'json') {
for (var property in response.data) {
if (response.data.hasOwnProperty(property)) {
if (property.toUpperCase().indexOf('DATE') > -1 || property.toUpperCase().indexOf('TIME') > -1) {
console.log(property.toUpperCase());
// Attempt to use JS built in date and validate
var tmpDate = new Date(response.data[property]);
if ($moment(tmpDate).isValid()) {
// Make it a valid date object if it has a valid date
response.data[property] = tmpDate;
} else {
// Make it undefined
response.data[property] = undefined;
}
}
}
}
}
Thing is, its always valid, so this code doesn't work.
EDIT: I could cheat and use tmpDate.getUTCFullYear() === 1 but i'd like to know how to do this the right way.

Meteor - can't pass variables in subscribe

I'm having problems with Meteor's subscribe. My goal is to search through my collection given criteria from user input form and spit back documents that match. My code works when I hard code let's say Meteor.subscribe('byProductAndBrand',1, 2) or whatever number instead of day and month. I tried the same numbers for day and month from the website, and it doesn't return any results. My variables seem to get the values from the html form because they print in the console, but for some reason they don't get passed in subscribe.Any suggestions?
The ScheduleList collection is just a bunch of documents with a certain hour, day, and month.
In the client:
Template.myform.events({
'click #submit' : function(event, template){
event.preventDefault();
Session.set('day', template.find('#day').value);
Session.set('month', template.find('#month').value);
var day = Session.get('day');
console.log(day);
var month = Session.get('month');
console.log(month);
var instance = Template.instance();
if(instance.byProductAndBrandHandle != null){
instance.byProductAndBrandHandle.stop();
}
instance.byProductAndBrandHandle = Meteor.subscribe('byProductAndBrand',day, month);
}
});
In the server:
Meteor.publish('byProductAndBrand', function(day, month){
var d = day;
var m = month;
return ScheduleList.find({day:d},{month: m});
});
The query limit in your server publication is incorrect, it should be:
ScheduleList.find({day:d, month: m});
Incidentally, one easy way to debug issues like this is to put a debugger; statement in your Meteor.publish function and then:
$ meteor debug
from the console. Then launch the Node Inspector in your browser at
http://localhost:8080/debug?port=5858
Then you can validate the parameter parsing and also double check your logic interactively. Sander's answer is correct though.

MooTools Date Picker default format

I am using the datepicker from mootools and trying to have it localised format depending on the users location.
For example:
08/15/13 //American
15.08.13 //English
The documentation states that it should change by default format: (*string*, defaults to the default localized format)
But using this code:
window.addEvent('domready', function(){
new Picker.Date($$('input'), {
timePicker: true,
positionOffset: {x: 5, y: 0},
pickerClass: 'datepicker_bootstrap',
useFadeInOut: !Browser.ie
});
});
It always uses the American format 08/15/13 (Month first).
I am testing by changing my Time Zone set in windows 7 Date and Time
I don't think DatePicker does that "out of the box". I am not sure there is a water-proof way to get user location or language codes in client side scripts.
I just tried :
var language = window.navigator.userLanguage || window.navigator.language;
console.log(language);
and got "sv" in Chrome and "en-GB" in Firefox, in the same machine.
I got about the same using php / HTTP-request header. Thought about getting the info from server-side. This question points HTTP_ACCEPT_LANGUAGE as the best way, BUT:
$_SERVER["HTTP_ACCEPT_LANGUAGE"] // gives me:
sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4 //in chrome
en-gb,en;q=0.5 //in firefox
If you look in the DatePicker.js file you have in the first lines:
---
name: Locale.en-US.DatePicker
description: English Language File for DatePicker
authors: Arian Stolwijk
requires: [More/Locale]
provides: Locale.en-US.DatePicker
...
This gives me a feeling that it is set to en-US. It will work if you set it manually though.
My suggestion is to follow Dimitar's suggestion and set it manually with Locale.use(). If you add for example Locale.use('sv-SE'); before you call DatePicker you will get date in format yyyy-mm-dd.
I did a script to read the client-side language to try to make it dynamical. It works but It's not tested. This assigns a language comparing the Mootools Locale.list() and the browser/javascript answer I posted above:
var language = window.navigator.userLanguage || window.navigator.language;
console.log(language); //gives the results I wrote above (sv/en-GB resp. Chrome/FF)
var list = Locale.list().length;
for (var i = 0; i < list; i++) {
if (Locale.list()[i] == language) { //check if it's a full match
Locale.use(language);
} else {
// go for the first part of the "sv-SE" and find a match
// this is a compromise, it gets the right language but might miss the right country
var langArray = language.split('-');
var locArray = Locale.list()[i].split('-');
for (var k = 0; k < list; k++) {
if (langArray[0] == locArray[0]) {
Locale.use(Locale.list()[i]);
}
}
}
}
console.log(Locale.getCurrent().name); // gives sv-SE/en-GB resp. Chrome/FF
new Picker.Date($$('input'), {
// etc...
If you thinks it's useful you can fine-tune it...
Fiddle here

Using a funcCall that retrieves multiple fields

Ello, I'm using jQuery-Validation-Engine and i don't know if this is currently possible but, Is there a way for you to possibly use the funcCall in a senario such as this.
I have to check a date range using 2 textboxes. I want to bind funcCall[dateRangeCheck] in such a way that I can get access
to 2 fields as opposed to just the one. The end result would would something like this.
dateRangeCheck = function (fields, rules, i, options) {
if (isDate(fields[0]) && isDate(fields[1])) {
if (!dateCompare(fields[0], fields[1])) {
return "* Invalid Date Range";
}
}
};
Or possibly all the fields that a utilizing this particular funcCall like this
dateRangeCheck = function (fields, rules, i, options) {
for (var i=0;i<fields.length;i++)
if(!isDate(fields[i]){
return "* Invalid Date Range";
}
}
};
Is there some way to accomplish this?
Well now jQuery-validation-engine does support date range checking. Just made the commit myself waiting for it to be reviewed. lol, But no to that second example as of now.
Here is the source. Have a nice day.
Update
The latest version does support group validation.

Categories

Resources