I use to automatically tag my e-mails with time based labels, on Gmail.
Can I simply use filters?
Maybe yes, but I didn't get to make them work when specifying only dates as criterias.
Maybe I'm idiot, but Gmail simply doesn't allow me to specify such filters.
So I tricked him with a small Google Apps Script, that tags emails with a current year and a current month nested labels.
Please note that it just works for 2017 and month names are in Italian.
I scheduled it to run once a minute.
function labelAllMessages() {
var label2017 = GmailApp.getUserLabelByName("2017");
var months = ["gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"];
var getNumberOfDaysByMonth = function(i) {
return (i==1) ? 28 : ( !(i>=7 ^ !(i&1)) ? 30:31 );
};
var getDateRangeForMonth = function(ind) {
var month = months[ind];
var range = {
start: new Date(2017, ind, 1).valueOf(),
end: new Date(2017, ind, getNumberOfDaysByMonth(ind)).valueOf(),
label: GmailApp.getUserLabelByName("2017/" + month)
};
return range;
};
var threads = GmailApp.search('-label:2017 -label:"before 2017"
…