Skip to content

Instantly share code, notes, and snippets.

@asclines
Last active March 15, 2017 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asclines/4a9cadfc6f022c503101a1ee726e83bd to your computer and use it in GitHub Desktop.
Save asclines/4a9cadfc6f022c503101a1ee726e83bd to your computer and use it in GitHub Desktop.
Slack slash command returning how long till Trump is out of office
Date.daysBetween = function (date1, date2) {
//Get 1 day in milliseconds
var one_day = 1000 * 60 * 60 * 24;
// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
// Calculate the difference in milliseconds
var difference_ms = date2_ms - date1_ms;
// Convert back to days and return
return Math.round(difference_ms / one_day);
}
function processEvent(event, callback) {
var today = new Date();
var leaves = new Date(2021, 0, 20); //01/20/2021 12:00:00
var days = Date.daysBetween(today, leaves);
console.log('Days till ' +
leaves.toLocaleDateString() + ': ' +
days);
var response = {
response_type: "in_channel",
text: `${days} days left until Trump leaves office.`
}
callback(null, response);
}
exports.handler = (event, context, callback) => {
const done = (err, res) => callback(null, {
statusCode: err ? '400' : '200',
body: err ? (err.message || err) : JSON.stringify(res),
headers: {
'Content-Type': 'application/json',
},
});
processEvent(event, done);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment