Skip to content

Instantly share code, notes, and snippets.

@imthenachoman
Created September 21, 2017 03:30
Show Gist options
  • Save imthenachoman/447010db1873d70bd8c8ff481aa71b7a to your computer and use it in GitHub Desktop.
Save imthenachoman/447010db1873d70bd8c8ff481aa71b7a to your computer and use it in GitHub Desktop.
gets all dates (like birthdays and anniversaries) from Google Contacts and sends an e-mail with their details
var EMAIL_TO = "...@gmail.com";
var NUM_ADDITIONAL_DAYS = 8;
var TODAY = new Date((new Date()).toDateString());
var ONE_DAY = 24*60*60*1000;
function getContactDates()
{
var todayYear = TODAY.getFullYear();
var todayNumber = TODAY.getTime();
// date we want to go back to for previous dates
var previousNumber = addDaysToDateNumber(todayNumber, NUM_ADDITIONAL_DAYS * -1);
// date we want to go forward to for future dates
var futureNumber = addDaysToDateNumber(todayNumber, NUM_ADDITIONAL_DAYS);
var allContacts = ContactsApp.getContacts();
var allContactDates = {};
var justDates = [];
for(var i = 0, numContacts = allContacts.length; i < numContacts; ++i)
{
var oneContact = allContacts[i];
var contactName = oneContact.getFullName();
var contactEMail = oneContact.getPrimaryEmail();
var contactPhoneNumber = oneContact.getPhones(ContactsApp.Field.MOBILE_PHONE);
var contactDates = oneContact.getDates();
var contactInformation = [];
if(contactEMail) contactInformation.push(contactEMail);
if(contactPhoneNumber.length) contactInformation.push(contactPhoneNumber[0].getPhoneNumber());
contactInformation = contactInformation.join(", ");
for(var j = 0, numDates = contactDates.length; j < numDates; ++j)
{
var oneDate = contactDates[j];
var dateLabel = oneDate.getLabel().toString().toLowerCase();
var dateDate = new Date(todayYear, oneDate.getMonth().ordinal(), oneDate.getDay());
var dateNumber = dateDate.getTime();
var age = todayYear - oneDate.getYear();
if(!(dateNumber in allContactDates))
{
justDates.push(dateNumber);
allContactDates[dateNumber] = {"dateDate" : dateDate, "contactDateDetails" : []};
}
var contactDateDetails = [];
contactDateDetails.push(contactName);
contactDateDetails.push(dateLabel);
contactDateDetails.push(age + " years");
if(contactInformation) contactDateDetails.push(contactInformation);
allContactDates[dateNumber]["contactDateDetails"].push(contactDateDetails.join('<br />'));
}
}
justDates.sort();
var message = "";
// print today's dates
message += printDateList(allContactDates, [todayNumber]);
message += '<hr />';
// print next NUM_ADDITIONAL_DAYS number of future dates
message += printDateList(allContactDates, justDates.filter(function(dateNumber)
{
return dateNumber > todayNumber && dateNumber <= futureNumber;
}));
message += '<hr />';
// print previous NUM_ADDITIONAL_DAYS number of past dates
message += printDateList(allContactDates, justDates.filter(function(dateNumber)
{
return dateNumber < todayNumber && dateNumber >= previousNumber;
}).reverse());
message += '<hr />';
message += '<hr />';
message += '<hr />';
// print all everything
message += printDateList(allContactDates, justDates);
MailApp.sendEmail({
"to" : EMAIL_TO,
"subject" : "Today's Special Dates - " + TODAY.toISOString().split("T")[0] + " - " + TODAY.toDateString(),
"htmlBody" : message
});
}
function printDateList(contactDates, printList)
{
var out = "";
for(var i = 0, numDates = printList.length; i < numDates; ++i)
{
var oneDate = contactDates[printList[i]];
var dateDate = oneDate["dateDate"];
var contactDateDetails = oneDate["contactDateDetails"];
out += dateDifference(TODAY, dateDate) + ' - ' + dateDate.toDateString();
out += '<ul><li>' + contactDateDetails.join('<br /><br /></li><li>') + '</li></ul>';
}
return out;
}
function addDaysToDateNumber(dateNumber, daysToAdd)
{
return dateNumber + (daysToAdd * 24 * 60 * 60 * 1000);
}
function dateDifference(startDate, endDate)
{
var numDaysBetween = Math.round((endDate.getTime() - startDate.getTime())/ONE_DAY);
if(numDaysBetween == 0) return "Today";
if(numDaysBetween == 1) return "Tomorrow";
if(numDaysBetween == -1) return "Yesterday";
if(numDaysBetween == 2) return "Day After Tomorrow";
if(numDaysBetween == -2) return "Day Before Yesterday";
if(numDaysBetween > 2) return "in " + numDaysBetween + " days";
return (numDaysBetween * -1) + " days ago";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment