How to get the names of days between two dates ionic+angularjs - ionic-framework

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));
})

Related

Calculate the difference of dates in days

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';
}
}

google script type error is not a function in date function

can someone help me figure what's wrong with my logic?, i'm new using google script app and stuck around a week and got this error
Error message:
TypeError: d1.getTime is not a function (inDays)
var DateDiff = {
inDays: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
Logger.log("t1" + t1)
return parseInt((t2-t1)/(24*3600*1000)+1);
}
}
var dateStr = data[m][xMonth].toString() // the value here is =>1/10/2022
var todayDate = new Date();
if(dateStr.toString().includes('/')){
var yearA = '20'+dateStr.substring(6, 8)
var monthA = +dateStr.substring(3, 5)
if(monthA.toString() != '11' || monthA.toString() != '12') monthA = '0'+monthA
var dayA = +dateStr.substring(0, 2)
var tempDate = yearA + monthA + dayA
var Ryear = +tempDate.substring(0, 4)
var Rmonth = +tempDate.substring(4, 6)
var Rday = +tempDate.substring(6, 8)
var newDate = new Date(Ryear, Rmonth - 1, Rday)//because month start from 0
var realDueDate = DateDiff.inDays(todayDate, newDate) // error here in var newDate
}
Try this:
function myFunk() {
const D2 = new Date();
const D1 = new Date(D2.getFullYear(),D2.getMonth() - 2,D2.getDate());
var DateDiff = {
d1:D1,d2:D2,
inDays: function () {
let t2 = this.d2.getTime();
let t1 = this.d1.getTime();
return parseInt((t2 - t1) / (24 * 3600 * 1000) + 1);
}
}
Logger.log(DateDiff.inDays());
}
Execution log
10:58:41 AM Notice Execution started
10:58:39 AM Info 62.0
10:58:41 AM Notice Execution completed

Creating calendar syncronised to spreadsheet but all events are created onn 1 Jan 1970 at 03:00. What did I miss?

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.

MaterializeCSS disable specific dates

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();
}
});

Datepicker noweekend and date range

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
});
});