Modify events in calendar to become recurring - calendar

I am trying to create a Google Script that goes through each event within a time range and modifies the events to a yearly recurring event. Currently, I mistakenly put in a bunch of birthdays into a calendar, but did not set them as recurring. Instead of going through each one manually, I wanted to create a script that sets them to a yearly recurrence. I'm getting stuck on line 12.
NOTE: Currently, I simply have my code searching for one specific event called "john bday" but would take out the "if" statement once I get it to modify the existing event correctly. Please advise and thank you.
function myFunction() {
var fromDate = new Date(2019,0,1,0,0,0); //This is January 1, 2019
var toDate = new Date(2019,2,31,0,0,0); //This is March 31, 2019
var calname = "testing calendar";
var findtitle = "john bday";
var calendar = CalendarApp.getCalendarsByName(calname)[0];
var events = calendar.getEvents(fromDate,toDate);
for (var i=0; i<events.length;i++) {
var ev = events[i];
if (ev.getTitle()==findtitle) {
CalendarApp.newRecurrence().addYearlyRule();
}
}
}

I was able to get it to work by copying the event into a new series and then deleting the original event. Thanks to all of those who took a look at this.
function myFunction() {
var fromDate = new Date(2018,6,1,0,0,0); //This is July 1, 2018
var toDate = new Date(2019,0,31,0,0,0); //This is January 31, 2019
var calname = "testing calendar";
var calendar = CalendarApp.getCalendarsByName(calname)[0];
var events = calendar.getEvents(fromDate,toDate);
var newRecurrence = CalendarApp.newRecurrence().addYearlyRule();
for (var i in events){
var ev = events[i];
var newEvent = calendar.createEventSeries(events[i].getTitle(), events[i].getStartTime(), events[i].getEndTime(), newRecurrence)
ev.deleteEvent();
}
}

Related

How to get specific month on AngularJS

How can I get the specific date month coming from the backend on angularJS
I wish to add something like this:
var curMonth = new Date().getMonth();
var monthData = vm.paymentsData[0].date.Date().getMonth();
if (curMonth == monthData) {
console.log ("Same Month");
}
Im getting error on:
var monthData = vm.paymentsData[0].date.Date().getMonth();
it says:
angular.js:14328 TypeError: vm.paymentsData[0].date.Date is not a function
Thanks
Data from the backend
I think your code should be written as follows:
var curMonth = new Date().getMonth();
// this maybe a string .. and you cannot call Date() function in that way.
var monthData = vm.paymentsData[0].date;
// Date function should be called this way.
var monData = (new Date(monthData)).getMonth();
if (curMonth == monData) {
console.log ("Same Month");
}

Aggregate GPM hourly data to daily in GEE

I need to aggregate the sum of 48 half-hourly images per day of a GPM collection getting an imageCollection with the band "precipitationCal" and daily images
I've tried to fill and iterate an empty featureCollection but I get an empty collection without images
var dataset = ee.ImageCollection('NASA/GPM_L3/IMERG_V05')
var startdate = ee.Date.fromYMD(2014,3,1)
var enddate = ee.Date.fromYMD(2014,4,1)
var precipitation = dataset.filter(ee.Filter.date(startdate,enddate)).select('precipitationCal')
print(precipitation)
var difdate = enddate.difference(startdate, 'day')
// Time lapse
var lapse = ee.List.sequence(0, difdate.subtract(1))
var startdate = ee.Date('2014-01-01')
var listdates = lapse.map(function(day){
return startdate.advance(day, 'day')
})
var pts = ee.FeatureCollection(ee.List([]))
var newft = ee.FeatureCollection(listdates.iterate(function(img, ft) {
// Cast
ft = ee.FeatureCollection(ft)
var day = ee.Date(img)
// Filter the collection in one day
var day_collection = precipitation.filterDate(day, day.advance(1, 'day'))
// Get the sum of all 24 images into one Image
var sum = ee.Image(day_collection.sum())
// Return the FeatureCollection with the new properties set
return sum
}, listdates))
Please have a try about my package pkg_trend. aggregate_prob function in it, works just like aggregate in R language.
var imgcol_all = ee.ImageCollection('NASA/GPM_L3/IMERG_V06');
function add_date(img){
var date = ee.Date(img.get('system:time_start'));
var date_daily = date.format('YYYY-MM-dd');
return img.set('date_daily', date_daily);
}
var startdate = ee.Date.fromYMD(2014,3,1);
var enddate = ee.Date.fromYMD(2014,4,1);
var imgcol = imgcol_all
.filter(ee.Filter.date(startdate,enddate)).select('precipitationCal')
.map(add_date);
// imgcol = pkg_trend.imgcol_addSeasonProb(imgcol);
print(imgcol.limit(3), imgcol.size());
var pkgs = require('users/kongdd/pkgs:pkgs.js');
var imgcol_daily = pkgs.aggregate_prop(imgcol, "date_daily", 'sum');
print(imgcol_daily);
Map.addLayer(imgcol_daily, {}, 'precp daily');
The GEE link is https://code.earthengine.google.com/3d8c7e68e0a7a16554ff8081880bfdad
According to GEE GPM page, This product is a half-hour precipitation rate. So, to get daily sum with the code above you should divide every image in collection by 2.

How to convert readable date time in timestamp which comes from backend

I want to convert the commit date and time to time stamp which I get from my APIs.
But I don't know how to do this in angular?
Here is my controller code :
var commitDate = item.commitMetaData.commitDate;
var dat = new Date(commitDate);
But it says "Invalid Date"
PS: Thanks in advance
What you could do is generate the date from the values montValue , year, dayOfMonth
with plain Javascript you could just do
var d = new Date();
d.setMonth(commitDate.monthValue +1); //see explanation below
d.setDate(commitDate.dayOfMonth);
d.setYear(commitDate.year);
be careful the months start at 0 so January is actually 0 so in your example you would have to add +1
You can also create a filter for this
.filter('createDate', function ($filter) {
return function (input) {
if (input != null && input != undefined) {
var d = new Date();
d.setMonth(input.monthValue +1); //see explanation below
d.setDate(input.dayOfMonth);
d.setYear(input.year);
return d;
}
};
})
and call it like
var commitDate = item.commitMetaData.commitDate;
var dat = $filter('createDate')(commitDate);
reference JS Date

Paginate By Week - AngularJS

I am trying to paginate a list of events (using ng-repeat) by week in AngularJS. I have a custom filter working that only displays the events within the current week, but I am trying to add functionality to look at future and past weeks.
Here is the filter I am using for the event lists -
$scope.week = function(item) {
var weekStart = moment().startOf('week');
var weekEnd = moment().endOf('week');
var eventTime = moment(item.jsdatetime);
if (eventTime >= weekStart && eventTime <= weekEnd) return true;
return false;
};
I have tried using ng-click to call a function that uses moment.js to .add(7, 'days'); to the weekStart and weekEnd variables but can't seem to get it to work.
Any help would be appreciated.
Here's a CodePen with the basic functionality going on - http://codepen.io/drewbietron/pen/xbKNdK
The moment() always return the current date/time.
You need to store a reference to it to a variable, and then use that for manipulations.
(and since you have other variables depending on it, i would create a function that sets all those variables at once)
So in the controller i changed the top part to
var currentDate,
weekStart,
weekEnd,
shortWeekFormat = 'MMMM Do';
function setCurrentDate(aMoment){
currentDate = aMoment,
weekStart = currentDate.clone().startOf('week'),
weekEnd = currentDate.clone().endOf('week')
}
// initialize with current date
setCurrentDate(moment());
// use these methods for displaying
$scope.currentWeek = function(){ return currentDate.format(shortWeekFormat); };
$scope.currentWeekStart = function(){ return weekStart.format(shortWeekFormat); };
$scope.currentWeekEnd = function(){ return weekEnd.format(shortWeekFormat); };
Then create two methods for going to next/previous week
$scope.nextWeek = function(){
setCurrentDate(currentDate.add(7,'days'));
};
$scope.prevWeek = function(){
setCurrentDate(currentDate.subtract(7,'days'));
};
(moment.js implements valueOf so you do direct comparisons)
And finally change your week filter to actually compare the dates (using .isSame(), .isBefore() and .isAfter()) instead of the moment objects (which was wrong as you cannot do direct comparisons on custom objects)
$scope.week = function(item) {
var eventTime = moment(item.jsdatetime);
if ((eventTime.isSame(weekStart) || eventTime.isAfter(weekStart))&&
(eventTime.isSame(weekEnd) || eventTime.isBefore(weekEnd))) return true;
return false;
};
$scope.week = function(item) {
var eventTime = moment(item.jsdatetime);
return (eventTime >= weekStart && eventTime <= weekEnd);
};
(you also, most likely, want the ng-repeat on the li elements and not the ul)
Demo at http://codepen.io/gpetrioli/pen/QwLRQB
The weekStart and weekEnd variables don't exist outside of the scope of week(item). If you're using ngClick to call a function that tries to modify those variables, it'll just return undefined. I don't know how your layout is but I would pull those two variables outside of the week function and make them $scope variables.
Additionally, I would have ngClick call a function that would change the two $scope variables (either adds 7 or subtracts 7 depending on which direction you want to go in).
$scope.weekStart = moment().startOf('week');
$scope.weekEnd = moment().endOf('week');
$scope.week = function(item) {
var eventTime = moment(item.jsdatetime);
if (eventTime >= $scope.weekStart && eventTime <= $scope.weekEnd) return true;
return false;
};
$scope.update= function(direction) {
$scope.weekStart.add(direction, 'days');
$scope.weekEnd.add(direction, 'days');
}
And create two buttons in your view:
Previous week
Next week

Angularjs - Time difference between my data time and CURRENT TIME

I am trying to display the time difference between my {{trade.timer}} and the current time but coudn't succeed after many tries.
I am looking to make the $scope.gains[i].timer = vtime - "CURRENTTIME"
Here is my code:
$scope.updatetimerValue = function (timerValue){
$.each(timerValue, function(k, v) {
for (var i =0; i < $scope.gains.length ; i ++) {
if($scope.gains[i].orderid == v.orderid){
$scope.gains[i].timer = v.time;
}
}
});
}
<td>{{gain.timer | date: 'HH:mm'}}</td>
Any idea?
Note: v.time time format is yyyy-MM-dd HH:mm:ss
You can get date difference between two days with basic javascript.
var date = new Date('10/27/2014');
var currentDate = new Date();
var milisecondsDiff = date-currentDate;
var secondsDiff = miliseconds/1000;
var minutesDiff = seconds/60;
var hoursDiff = minutes/60;
var daysDiff = hours/24;
Also I suggest don't mix up AngularJS and JQuery.
And instead $.each use the angular.forEach
angular.forEach(values, function(value, key) {
///
});
or even better to use simple for loop, because it works faster.

Resources