Skip to content

Instantly share code, notes, and snippets.

@fadoaglauss
Last active September 11, 2020 20:41
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 fadoaglauss/4953962edddf06d0e691176dd57bfa37 to your computer and use it in GitHub Desktop.
Save fadoaglauss/4953962edddf06d0e691176dd57bfa37 to your computer and use it in GitHub Desktop.
Gets the next day that you will send an automatic message to your clients considering timezone, service start time, working days and holidays.
function nowUTC(offset) {
let now = new Date;
let utc_timestamp = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),
now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());
return new Date(utc_timestamp + offset * 3600 * 1000);
}
function formatDay(day) {
return day < 10 ? `0${day}` : day;;
}
function formatMonth(month) {
month = parseInt(month) + 1;
return month < 10 ? `0${month}` : month;
}
function isYearLeap(year) {
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
function numberOfDaysInMonth(month, isYearLeap) {
let numberOfDaysInMonth = [];
if (isYearLeap) {
numberOfDaysInMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
} else {
numberOfDaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
}
return numberOfDaysInMonth[month];
}
function nextWeekDay(weekday) {
let firstWeekDay = 0;
let lastWeekDay = 6;
// Se é o ultimo dia da semana: 0..6
if (weekday == lastWeekDay) {
return firstWeekDay;
}
return weekday + 1;
}
function nextDay(day, numberOfDaysInMonth) {
let firstDayOfMonth = 1;
// Se é o ultimo dia do mês
if (day == numberOfDaysInMonth) {
return firstDayOfMonth;
}
return day + 1;
}
function nextMonth(day, month, numberOfDaysInMonth) {
let firstMonthInYear = 0;
let lastMonthInYear = 11;
// Se é ultimo dia do mês
if (day == numberOfDaysInMonth) {
//Se é o ultimo mes do ano
if (month == lastMonthInYear) {
return firstMonthInYear;
}
return month + 1;
}
return month;
}
function nextYear(day, month, year, numberOfDaysInMonth) {
let lastMonthInYear = 11;
// Se é o último dia do mês & ultimo mês do ano: 31/12
if (day == numberOfDaysInMonth && month == lastMonthInYear) {
return year + 1;
}
return year;
}
function isHoliday(day, month, holidays) {
day = formatDay(day);
month = formatMonth(month);
if (holidays.includes(`${day}/${month}`)) {
return true;
}
return false;
}
function isWorkDays(weekday, workdays) {
if (workdays.includes(weekday)) {
return true;
}
return false;
}
function dateToSendNextMessage(timezone, serviceStartTime, workingDays, holidays) {
let today = nowUTC(timezone);
let day = today.getUTCDate();
let weekday = today.getUTCDay();
let month = today.getUTCMonth();
let year = today.getUTCFullYear();
let _numberOfDaysInMonth = numberOfDaysInMonth(month, isYearLeap(year));
while (true) {
year = nextYear(day, month, year, _numberOfDaysInMonth);
month = nextMonth(day, month, _numberOfDaysInMonth);
weekday = nextWeekDay(weekday);
day = nextDay(day, _numberOfDaysInMonth);
_numberOfDaysInMonth = numberOfDaysInMonth(month, isYearLeap(year));
if (!isHoliday(day, month, holidays) && isWorkDays(weekday, workingDays)) {
day = formatDay(day);
month = formatMonth(month);
return `${year}-${month}-${day}T${serviceStartTime}:00Z`; //Format: 2020-09-10T12:25:00.000Z
}
}
}
@fadoaglauss
Copy link
Author

fadoaglauss commented Sep 11, 2020

Params format:

  • timezone: "-3"
  • serviceStartTime: "7:00"
  • workingDays: "0,1,2,3,4,5,6" where 0 is Sunday
  • holidays: "01/01|21/04|01/05|07/09|12/10|02/11|15/11|25/12" in which these are brazilian national holidays

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment