How to disable dates in Dynarch calendar - javascript

I'm using Dynarch calendar in one of my Magento module and i want to disable some specific dates in an array like below.
var array = ["2014-01-14","2014-01-15","2011-01-16"]
I'm currently using this code and this disables all the days except sundays. I tried in many ways with JQuery methods and could not be success.
disableFunc : function(date)
{
var y = calendar.date.getFullYear();
var m = calendar.date.getMonth();
var d = calendar.date.getDate();
var day_off_array = dayoff.split(",") ;
//document.write(day_off_array);
currentTime = new Date();
var d1=currentTime.getDate();
var m1=currentTime.getMonth();
var y1=currentTime.getFullYear();
if (date.getDay() != 0) {
return true; // true says "disable"
} else {
return false; // leave other dates enabled
}
if(y < y1)
{
return true;
}
else if(m1 > m && y==y1)
{
return true;
}
}
Is there a way to achieve this and any helps would be appreciated. Thanks.

See examples at http://www.dynarch.com/jscal/#sec8
The above calendar allows selection between 8 April and 25 December 2009.
Here's the code that we used:
Calendar.setup({
cont: "sample1",
min: 20090408,
max: 20091225
});

Related

Disable past date in FullCalendar for dateClick

Those who still looking for a solution on how to disable past date click on Dateclick event on FullCalendar. can try see solution below, if any of the reader had other idea can post other solution below. It would be much better if there is another easy way. The more solution, the better it would be.
It's possible to improve on your string-manipulation of dates, by doing a simple comparison as follows:
dateClick: function (info) {
var clickedDate = getDateWithoutTime(info.date);
var nowDate = getDateWithoutTime(new Date())
if (clickedDate >= nowDate) alert("Future date");
else alert("Past date");
}
It is helped by this function:
//get date without the time of day
function getDateWithoutTime(dt)
{
dt.setHours(0,0,0,0);
return dt;
}
Demo: https://codepen.io/ADyson82/pen/ZErwJGx
One other option to achieve the same goal is to use the validRange functionality. This lets you dynamically set a start date, before which all dates are disabled:
validRange: function (nowDate) {
return {
start: nowDate
};
},
Demo: https://codepen.io/ADyson82/pen/VwQgWJO
The slight downside of this though is that it also hides any events which occur before the current date - you may not want this.
Therefore, another alternative is to use selectAllow to restrict where the user can select. Note that this relies on you using the select callback rather than dateClick - but that should not be a big problem.
selectAllow: function (info) {
return (info.start >= getDateWithoutTime(new Date()));
}
Demo: https://codepen.io/ADyson82/pen/VwQgzZJ
Below is just my current solution to disable past date on FullCalendar. Any idea or improvement would be appreciate to improve the code below.
dateClick: function(e) {
// Get Today
var dt = calendar.getDate();
var year = dt.getFullYear();
var month = dt.getMonth() + 1;
var day = dt.getDate();
if(month < 9){
month = '0'+month;
}
if(day < 9){
day = '0'+day;
}
var today = year + '-' + month + '-' + day;
// Get Selected Date
var check = e.dateStr;
let A = today.toString();
let B = check.toString();
alert('Date A: ' + A );
alert('Date B: ' + B );
if(B >= A){
alert('Past Date');
}
else{
alert('Future Date');
}
},

jquery datepicker: Enable specific date pattern

I have a specific need in my project where I should enable only 1st and the 15th of every month in the jquery date picker. I have seen several solutions that is based on an array of dates, but that is not going to work for me. Is there a way a pattern can be applied?
#mtndesign - For your response/question on why I cannot use an array:
The datepicker should always (all past, current and future months & years) show only 1 or 15. How many dates I can keep in the array? How do I create array by the user behavior for the user selected month? I felt that it will be complicated if I start creating an array
I guess you want to accomplish something like this:
function isAvailable(date) {
var dt = date.getDate();
if (dt == 1 || dt == 15) {
return [true,"",""];
} else {
return [false,"",""];
}
}
$('#some_div').datepicker({ beforeShowDay: isAvailable });
A more general solution would be:
var dates_avail = [1, 15, 23, 27];
function isAvailable(date) {
var dt = date.getDate();
if (dates_avail.indexOf(dt) != -1) {
return [true,"",""];
} else {
return [false,"",""];
}
}
$('#some_div').datepicker({ beforeShowDay: isAvailable });
Or to check specific dates of specific years:
var dates_avail = ["1/1/2016", "21/4/2016", "19/3/2016"];
function isAvailable(date) {
var dt = date.getDate();
dt += "/" + (date.getMonth()+1);
dt += "/" + date.getFullYear();
if (dates_avail.indexOf(dt) != -1) {
return [true,"",""];
} else {
return [false,"",""];
}
}
$('#some_div').datepicker({ beforeShowDay: isAvailable });
I want to use this function, is this format correct please?
<script type="text/javascript">
function isAvailable(date) {
var dt = date.getDate();
if (dt == 1 || dt == 16) {
return [true, "", ""];
} else {
return [false, "", ""];
}
}
$('#date2').datepicker({ beforeShowDay: isAvailable });
</script>

Get Month as well as date in my variable - javascript

Hi i am trying to do a IF statement which allows the current date to be compared to the input date.. if the input date is below the current date it will be false.
I have got the date passing through my variable but it only stores the number so for example it compares day 9 to another day, which is not very reliable. I want the variable to take in the month and the year as well, meaning it can compare the ENTIRE DATE.
If there is a better way let me know.
Here is my code
if (this.element.find('#visitdate').length > 0) {
var dateParts = $('#visitdate').val().split('/');
var check = new Date(dateParts[2], dateParts[1], dateParts[0], 0,0,0,0).getDate();
var today = new Date().getDate;
if (check < today) {
_errMsg = "Please enter a furture visit date";
return false;
} else {
return true;
}
}
Your line for today's date contains an error:
var today = new Date().getDate;
should be
var today = new Date().getDate();
format as mm/dd/yyyy
var from = '08/19/2013 00:00'
var to = '08/12/2013 00:00 '
var today = new Date().getDate();
function isFromBiggerThanTo(dtmfrom, dtmto){
var from = new Date(dtmfrom).getTime();
var to = new Date(dtmto).getTime() ;
return from >= to ;
}
or using below
var x = new Date('2013-05-23');
var y = new Date('2013-05-23');
and compare
You can try this - it's working fine in my project -
Step 1
First Create javascript function as below.
Date.prototype.DaysBetween = function () {
var intMilDay = 24 * 60 * 60 * 1000;
var intMilDif = arguments[0] - this;
var intDays = Math.floor(intMilDif / intMilDay);
if (intDays.toLocaleString() == "NaN") {
return 0;
}
else {
return intDays + 1;
}
}
Step 2
-
var check = new Date(dateParts[2], dateParts[1], dateParts[0], 0,0,0,0).getDate();
var today = new Date().getDate;
var dateDiff = check .DaysBetween(today);
// it will return integer value (difference between two dates )
if(dateDiff > 0 ){ alert('Your message.......');}
You can have this much easier.
You dont need to check with getDate() property you can just compare 2 dates.
And also is not needed to initialize with hours, minutes and seconds the Date, you only need year, month and date.
Here you have your example simplified
var dateParts = $('#visitdate').val().split('/');
var check = new Date(dateParts[2], dateParts[1], dateParts[0]);
var today = new Date();
if (check < today) {
return false;
} else {
return true;
}
http://jsfiddle.net/wns3LkLv/
Try this:
var user="09/09/2014/5/30";
var arrdt= user.split("/");
var userdt = new Date(arrdt[2], arrdt[1] - 1, arrdt[0],arrdt[3],arrdt[4]);
var currdt = new Date();
if (userdt < currdt) {
alert("userdate is before current date"); //do something
}else{
alert("userdate is after current date"); //do something
}
Thanks for all your answers guys i have fixed it.
I used the getTime function instead of getDate.
Then the check variable had to have a -1 assigned to the month as it was going 1 month to high.
var check = new Date(dateParts[2], dateParts[1]-1, dateParts[0], 0,0,0,0).getTime();
Cheers

check whether the date entered by the user is current date or the future date

I was browsing through the net to find a javascript function
which can check whether the date entered by the user is current date or the future date but i didn't found a suitable answer so i made it myself.Wondering If this can be achieved by one line code.
function isfutureDate(value)
{
var now = new Date;
var target = new Date(value);
if (target.getFullYear() > now.getFullYear())
{
return true;
}
else if(target.getFullYear() == now.getFullYear())
{
if (target.getMonth() > now.getMonth()) {
return true;
}
else if(target.getMonth() == now.getMonth())
{
if (target.getDate() >= now.getDate()) {
return true;
}
else
{
return false
}
}
}
else{
return false;
}
}
You can compare two dates as if they were Integers:
var now = new Date();
if (before < now) {
// selected date is in the past
}
Just both of them must be Date.
First search in google leads to this: Check if date is in the past Javascript
However, if you love programming, here's a tip:
A date formatted like YYYY-MM-DD could be something like 28-12-2013.
And if we reverse the date, it is 2013-12-28.
We remove the colons, and we get 20131228.
We set an other date: 2013-11-27 which finally is 20131127.
We can perform a simple operation: 20131228 - 20131127
Enjoy.
here's a version that only compares the date and excludes the time.
Typescript
const inFuture = (date: Date) => {
return date.setHours(0,0,0,0) > new Date().setHours(0,0,0,0)
};
ES6
const inFuture = (date) => {
return date.setHours(0,0,0,0) > new Date().setHours(0,0,0,0)
};
try out this
function isFutureDate(idate){
var today = new Date().getTime(),
idate = idate.split("/");
idate = new Date(idate[2], idate[1] - 1, idate[0]).getTime();
return (today - idate) < 0 ? true : false;
}
Demo
console.log(isFutureDate("02/03/2016")); // true
console.log(isFutureDate("01/01/2016")); // false
ES6 version with tolerable future option.
I made this little function that allows for some wiggle room (incase data coming in is from a slightly fast clock for example).
It takes a Date object and toleranceMillis which is the number of seconds into the future that is acceptable (defaults to 0).
const isDistantFuture = (date, toleranceMillis = 0) => {
// number of milliseconds tolerance (i.e. 60000 == one minute)
return date.getTime() > Date.now() + toleranceMillis
}
try this
function IsFutureDate(dateVal) {
var Currentdate = new Date();
dateVal= dateVal.split("/");
var year = Currentdate.getFullYear();
if (year < dateVal[2]) {
return false;//future date
}
else {
return true; //past date
}
}
In my case, I used DD-MM-YYYY format dates to compare and it gives an error since the behaviour of "DD-MM-YYYY" is undefined. So I convert it to a compatible format and compare it. And also if you need to compare only the dates and not time, you need to set time parameters to zero.
var inputDateVal = "14-06-2021";
if (inputDateVal != null && inputDateVal != '') {
var dateArr = inputDateVal.split("-");
var inputDate = new Date('"' + dateArr[2] + "-" + dateArr[1] + "-" + dateArr[0] + '"').setHours(0, 0, 0, 0);
var toDay = new Date().setHours(0, 0, 0, 0);
if(inputDate > toDay){
console.log("Date is a future date");
}else if(inputDate== toDay){
console.log("Date is equal to today");
}else{
console.log("Date is a past date");
}
}
You can use moment.js library
let dateToBeCompared = "10/24/2021"; // "DD/MM/YYYY" format
// For past dates
moment(dateToBeCompared, "DD/MM/YYYY").isBefore(moment(new Date(), "DD/MM/YYYY"),
'day')
// For same dates
moment(dateToBeCompared, "DD/MM/YYYY").isSame(moment(new Date(), "DD/MM/YYYY"),
'day')
// For future dates
moment(dateToBeCompared, "DD/MM/YYYY").isAfter(moment(new Date(), "DD/MM/YYYY"),
'day');
There are other functions like also like isSameOrAfter() and isSameOrBefore()
Have a look at here

How to check if any date from array is in date range?

Hope you are all well.
I need to check if any date from array
var arrayDates = ["2013-07-26", "2013-07-27"];
is in date range of
var startDate = new Date("2013-07-10");
var endDate = new Date("2013-07-10");
I am really stuck and started to confuse myself. Can anyone help me with that please.
P.S. Dates above are for example, they will be dynamic.
Thank you!
You will need to use real date objects rather than strings.
maybe have a look at using dateJs for parsing dates
http://www.datejs.com/
But really you need to iterate through the array of dates and check if they fall between the tick value of your start and end dates.
Try this:
var arrayDates = [];
arrayDates.push(new Date(2013, 7 , 26));
arrayDates.push(new Date(2013, 7 , 27));
var startDate = new Date("2013-07-10");
var endDate = new Date("2013-07-10");
for(i = 0; i < arrayDates.length; i++){
if(arrayDates[i] >= startDate && arrayDates[i] <= endDate) {
alert('Yes');
}
}
Another method - http://jsfiddle.net/Mh5vn/
var ds = ["2013-07-26", "2013-07-27"];
Array.prototype.between = function(arg) {
var d1 = new Date(this[0]),
d2 = new Date(this[1]),
d3 = new Date(arg);
return (d1.getTime() <= d3.getTime() && d3.getTime() <= d2.getTime());
}
console.log( ds.between('2013-07-26') );
// true
console.log( ds.between('2013-07-28') );
// false
After you have the date objects you can compare them in a pretty straight forward way. See this link at the bottom.
I see your question is tagged jquery, so you could do something like this:
$.each(arrayDates, function(i, val) {
if (val > endDate || val < startDate)
{
//Date is outside of that range
}
});
Hopefully you can convert these dates to numbers and compare them, here an example :
var arrayDates = ["2013-07-26", "2013-07-27"];
var unDash = function (string) {
return string.replace(/-/g, "")
}
var dateInRange = function (date, startDate, endDate) {
date = unDash(date)
startDate = unDash(startDate)
endDate = unDash(endDate)
return date > startDate && date < endDate
}
// You now filter your array to retrieve your dates
var dates = arrayDates.filter(function (date) {
return dateInRange(date, '2013-07-10', '2013-07-31')
})

Categories