I'm trying to disable the weekends (saturdays and sundays) and some dates (like '2018-05-11', '2018-05-21', '2018-05-24', etc.) in MaterializeCSS datepicker, but I want to disable those dates every year. I'm able to disable the days and dates with
$('.datepicker').pickadate({
disable: [
1, 7, new Date(2018,5,11), {from: [2018,5,11], to: [2018,5,22]}, {from: [2018,8,5], to: [2018,8,17]}
]
});
So I want to know if there's a way to made it repeat every year.
materializecss pickers
You can customize which days are disabled with the disableDayFn option when initializing the datepicker. This CodePen shows an example of the disableDayFn option being used to disable weekends, May 11th, 21st, and 24th of every year.
$('.disablePast').pickadate({
selectMonths: true, // Creates a dropdown to control month
selectYears: 105, // Creates a dropdown of 15 years to control year,
today: 'Today',
clear: 'Clear',
close: 'Ok',
format : 'dd/mm/yyyy',
closeOnSelect: true,
min : true
});
$('.datepicker').pickadate({
selectMonths: true, // Creates a dropdown to control month
selectYears: 105, // Creates a dropdown of 15 years to control year,
today: 'Today',
clear: 'Clear',
close: 'Ok',
format : 'dd/mm/yyyy',
closeOnSelect: true,
onSet: function(context) {
var id = this.$node[0].id;
var d = new Date(context.select);
var dd = d.getDate();
var mm = d.getMonth()+1; //January is 0!
var yyyy = '';
yyyy = new Date().getFullYear();
year1=yyyy;
month1=mm;
if(dd<10){
dd='0'+dd;
}
if(mm<10){
mm='0'+mm;
}
var df = mm+'/'+dd+'/'+yyyy;
var msec = Date.parse(df);
var d1 = new Date();
var d2 = new Date(msec);
var timeDiff = d2.getTime() - d1.getTime();
var DaysDiff = timeDiff / (1000 * 3600 * 24);
var dff = '';
var dft = '';
if(DaysDiff >= 0){
dff = dd+'/'+mm+'/'+yyyy;
} else{
yyyy = yyyy + 1;
dff = dd+'/'+mm+'/'+yyyy;
}
if(month1==1)
{
year1 = year1 - 1;
month1 = 12;
}
else if(month1<=10)
{
month1=month1-1;
month1='0'+month1;
}
else
{
month1=month1-1;
}
dft = dd+'/'+month1+'/'+year1;
/*if(df > 0){
dft = dd+'/'+mm+'/'+yyyy;
}
// if(mm = 01) {
// mm = 12;
// yyyy = yyyy - 1;
// dft = dd+'/'+mm+'/'+yyyy;
// }
else{
yyyy = yyyy + 1;
mm = mm - 1;
dft = dd+'/'+mm+'/'+yyyy;
}*/
console.log(dft);
if(id == 'dob')
$('#dob_rem_on').val(dff);
if(id == 'aniv_date')
$('#aniv_rem_on').val(dff);
if(id == 'passport_ren')
$('#pass_rem_on').val(dft);
if(id == 'insurance_ren')
$('#insurance_rem_on').val(dft);
if(id == 'life_insurance_ren')
$('#life_insurance_rem_on').val(dft);
Materialize.updateTextFields();
}
});
Related
I need to calculate the difference in days between two dates in two differente occasions
Occasion 1 - There's a start and an end date
Occasion 2 - There's a start date and a "IN PROGRESS" where the end date should be
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var daTa = ss.getSheetByName("Data");
var daysColumn = daTa.getRange('C2:C' + daTa.getLastRow()).getValues();
var startDate = daTa.getRange('C2:C'+ daTa.getLastRow()).getValues().flat();
var endDate = daTa.getRange('A2:A'+ daTa.getLastRow()).getValues().flat();
var today = new Date().valueOf();
endDate.forEach((finaldate,row) => {
if(finaldate == "IN PROGRESS") {
daysColumn[row][0] = (parseInt(startDate,10)-today);
} else {
daysColumn[row][0] = (parseInt(startDate,10)-parseInt(finaldate,10));
}})
daTa.getRange(2,4,daysColumn.length, 1).setValues(daysColumn)
}
Right now i got this bit of code, and it know what needs to be done, but it returns only "#NUM!" values on the column, where it should print the numbers.
Probably you want this:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var daTa = ss.getSheetByName("Data");
var daysColumn = daTa.getRange('C2:C' + daTa.getLastRow()).getValues();
var startDate = daTa.getRange('C2:C' + daTa.getLastRow()).getValues().flat()
.map(x => new Date(x).valueOf()); // get milliseconds of the dates
var endDate = daTa.getRange('A2:A' + daTa.getLastRow()).getValues().flat()
.map(x => new Date(x).valueOf()); // get milliseconds of the dates
var today = new Date().valueOf();
var day = 1000 * 60 * 60 * 24; // milliseconds in a day
endDate.forEach((finaldate, row) => {
if (finaldate == "IN PROGRESS") {
daysColumn[row][0] = (startDate[row] - today) / day;
} else {
daysColumn[row][0] = (startDate[row] - finaldate) / day;
}
})
daTa.getRange(2, 4, daysColumn.length, 1).setValues(daysColumn)
}
function DiffInDays(Day1,Day2) {
if(Day1 && Day2 && (Object.prototype.toString.call(Day1) === '[object Date]') && (Object.prototype.toString.call(Day2) === '[object Date]')) {
var day=86400000;
var t1=new Date(Day1).valueOf();
var t2=new Date(Day2).valueOf();
var d=Math.abs(t2-t1);
var days=Math.floor(d/day);
//Logger.log(days);
return days;
} else {
throw 'Invalid Inputs';
}
}
I am making a spreadsheet that requires finding out if a certain cell range has been edited and then add the date that it was edited on. I have the range of cells needed to be edited, and I also don't want the date to change if I sort the table from a different column.
I would think the general idea would look something like this, not actual code just an idea:
cell_range=(b2:d2)
if (cell_range)==editted:
date()
if (table_sort==True):
date_change==False
If anyone knows how to do this, that would be greatly appreciated.
you need a script for this. try for example:
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sheet1" ) {
var r = s.getActiveCell();
if( r.getColumn() == 2 ) {
var nextCell = r.offset(0, 8);
var newDate = Utilities.formatDate(new Date(),
"GMT+8", "MM/dd/yyyy");
nextCell.setValue(newDate);
}
if( r.getColumn() == 3 ) {
var nextCell = r.offset(0, 7);
var newDate = Utilities.formatDate(new Date(),
"GMT+8", "MM/dd/yyyy");
nextCell.setValue(newDate);
}
if( r.getColumn() == 4 ) {
var nextCell = r.offset(0, 6);
var newDate1 = Utilities.formatDate(new Date(),
"GMT+8", "MM/dd/yyyy");
nextCell.setValue(newDate1);
}}}
"Sheet1" = sheet name
r.getColumn() == 3 = column C / 3rd column
r.offset(0, 7) = offset timestamp 7 columns to the right on the same row eg column H
"GMT+8" = timezone
"MM/dd/yyyy hh:mm:ss" = date and time format
alternative (untested):
function onEdit(e) {
if ([2].indexOf(e.range.columnStart) != -1) {
e.range.offset(0, 7).setValue(new Date()); }
if ([3].indexOf(e.range.columnStart) != -1) {
e.range.offset(0, 6).setValue(new Date()); }
if ([4].indexOf(e.range.columnStart) != -1) {
e.range.offset(0, 5).setValue(new Date()); } }
I am following the conversation and tutorial in post Create Google Calendar Events from Spreadsheet but prevent duplicates
But I cannot get the final solution to work. It fills my calendar but all dates are 1/1/1970 at 3 am.
The code is as follows:
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name: "Export Events",
functionName: "exportEvents"
}];
sheet.addMenu("Calendar Actions", entries);
};
function parseDate(s) {
var months = {
jan: 0, feb: 1, mar: 2, apr: 3, may: 4, jun: 5, jul: 6, aug: 7, sep: 8, oct: 9, nov: 10, dec: 11
};
var p = s.replace(".", "").split('-');
return new Date(p[2], p[1], p[0]);
}
/**
* Export events from spreadsheet to calendar
*/
function exportEvents() {
var sheet = SpreadsheetApp.getActiveSheet();
var headerRows = 1; // Number of rows of header info (to skip)
var range = sheet.getDataRange();
var data = range.getDisplayValues();
var calId = "the calendar"; // PRODUCTION
//var calId = "the calendar; // TEST
var cal = CalendarApp.getCalendarById(calId);
//Logger.log(cal);
//Logger.log(data.length);
for (i = 0; i<data.length; i++) {
if (i<headerRows) continue; // Skip header row(s)
if (data[i][0].length<1) continue; // Skip if no content.
var row = data[i];
Logger.log(row);
var date = parseDate(row[0]); // First column
//Logger.log(date);
var title = row[1]; // Second column
var tstart = new Date();
var s = row[2].split(":");
tstart.setHours(s[0]);
tstart.setMinutes(s[1]);
tstart.setSeconds(s[2]);
tstart.setDate(date.getDate());
tstart.setMonth(date.getMonth());
tstart.setYear(date.getYear());
var tstop = new Date();
var e = row[3].split(":");
tstop.setHours(e[0]);
tstop.setMinutes(e[1]);
tstop.setSeconds(e[2]);
tstop.setDate(date.getDate());
tstop.setMonth(date.getMonth());
tstop.setYear(date.getYear());
var loc = row[4];
var desc = row[5];
var id = row[6]; // Sixth column == eventId
// Check if event already exists, update it if it does
var event = null;
if (id.length > 0) {
try {
event = cal.getEventSeriesById(id);
} catch (e) {
// do nothing - we just want to avoid the exception when event doesn't exist
}
}
if (!event) {
//cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
var newEvent = cal.createEvent(title, tstart, tstop, {
description: desc, location: loc
}).getId();
var r = i + 1;
var cell = sheet.getRange("G" + r);
cell.setValue(newEvent);
} else {
Logger.log(event);
event.setTitle(title);
event.setDescription(desc);
event.setLocation(loc);
// event.setTime(tstart, tstop); // cannot setTime on eventSeries.
// ... but we CAN set recurrence!
var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
event.setRecurrence(recurrence, tstart, tstop);
}
debugger;
}
}
I think I must be making an obvious rookie mistake but cannot figure it out. Any chance of some help?
BTW: my dates are formatted: 12/01/2019 and times formatted: 18:00:00.
I try to disabled the date for holidays and weekend but that not work
My code :
$(function() {
$('.thing12DatePicker').datepicker(
{
beforeShowDay: function (date) {
var startDate = "2017-06-17",
endDate = "2017-06-21",
dateRange = [];
for (var d = new Date(startDate); d <= new Date(endDate); d.setDate(d.getDate() + 1)) {
dateRange.push($.datepicker.formatDate('yy-mm-dd', d)&& $.datepicker.noWeekends);
}
var dateString = jQuery.datepicker.formatDate('yy-mm-dd', date);
return [dateRange.indexOf(dateString) == -1];
}
});
});
The date holidays are disabled but not the weekend !
I want disbled the weekend .
thx
I have found solution
var startDate = "2017-06-17",
endDate = "2017-06-21",
bankHoliDays = [];
for (var d = new Date(startDate); d <= new Date(endDate); d.setDate(d.getDate() + 1)){
bankHoliDays.push($.datepicker.formatDate('yy-mm-dd', d));
}
function disableDates(date) {
var dt = $.datepicker.formatDate('yy-mm-dd', date);
var noWeekend = jQuery.datepicker.noWeekends(date);
return noWeekend[0] ? (($.inArray(dt, bankHoliDays) < 0) ? [true] : [false]) : noWeekend;
}
$(function () {
$('#datepicker').datepicker({
beforeShowDay: disableDates
});
});
After using this function to get the number of days I want to get the names of days between two dates ionic+angularjs.
$scope.dayDiff = function (date1, date2) {
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
$scope.dayDifference = Math.ceil(timeDiff / (1000 * 3600 * 24));
alert($scope.dayDifference);
}
Here's how I did it. View Plunkr Demo
angular.module('app', [])
.controller('MainCtrl', function($scope) {
var days = function(date1, date2) {
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diff = Math.ceil(timeDiff / (1000 * 3600 * 24));
var _days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var days = [];
for (var i = 0; i < diff; i++) {
date1.setDate(date1.getDate() + i);
days.push(_days[date1.getDay()]);
}
return days;
};
$scope.days = days(new Date(2016, 05, 01), new Date(2016, 05, 05));
})