Last active
August 29, 2015 14:02
-
-
Save yssharma/517e2a9448a53cda0baf to your computer and use it in GitHub Desktop.
Get next G+ hangout : 1st Tuesday of month
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////////////////////////////// | |
// Version 1 | |
////////////////////////////////////////////////////////////////////////////////// | |
var getFirstTuesdayOfMonth = function(year, month){ | |
var date = new Date(year, month, 1, 0, 0, 0, 0); | |
if(date.getDay() < 3){ | |
date.setDate(3-date.getDay()); | |
} | |
else{ | |
date.setDate(7+(3-date.getDay())); | |
} | |
return date; | |
}; | |
var getNextHangout = function(){ | |
months = new Array("January","February","March","April","May","June","July","August","September","October","November","December"); | |
var today = new Date(); | |
nexthangout=getFirstTuesdayOfMonth(today.getFullYear(), today.getMonth()); | |
message=""; | |
if(today.getTime() > nexthangout.getTime()){ | |
nexthangout=getFirstTuesdayOfMonth(today.getFullYear(), today.getMonth()+1); | |
} | |
message = nexthangout.getDate() + getSuffix(nexthangout.getDate()) + " " + months[nexthangout.getMonth()]; | |
return message; | |
}; | |
function getSuffix(n) { | |
var d = (n|0)%100; | |
return d > 3 && d < 21 ? 'th' : ['th', 'st', 'nd', 'rd'][d%10] || 'th'; | |
}; | |
alert(getNextHangout()); | |
////////////////////////////////////////////////////////////////////////////////// | |
// Version 2 | |
////////////////////////////////////////////////////////////////////////////////// | |
function getNextTuesday() { | |
months = new Array("January","February","March","April","May","June","July","August","September","October","November","December"); | |
var d = new Date(); | |
datetoday = d.getDate(); | |
d.setDate(1); | |
// Hangout this month | |
while (d.getDay() !== 2) { | |
d.setDate(d.getDate() + 1); | |
} | |
/** Hangout Today **/ | |
if(datetoday == d.getDate()){ | |
nexthangout = new Date(d.getTime()); | |
return "Next Hangout Today. See you there."; | |
} | |
/** Hangout this month **/ | |
else if(datetoday < d.getDate()){ | |
nexthangout = new Date(d.getTime()); | |
} | |
/** Hangout next month **/ | |
else{ | |
month = d.getMonth() + 1; | |
d.setDate(1); | |
d.setMonth(d.getMonth() + 1); | |
// Get the first Tuesday in next month | |
while (d.getDay() !== 2) { | |
d.setDate(d.getDate() + 1); | |
} | |
nexthangout = new Date(d.getTime()); | |
} | |
datestr = nexthangout.getDate() + getSuffix(nexthangout.getDate()) + " " + months[nexthangout.getMonth()]; | |
return "Next Hangout: " + datestr + ". See you there."; | |
} | |
function getSuffix(n) { | |
var d = (n|0)%100; | |
return d > 3 && d < 21 ? 'th' : ['th', 'st', 'nd', 'rd'][d%10] || 'th'; | |
}; | |
alert(getNextTuesday()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment