Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/utils/payday.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { HOURS_IN_DAY, MINUTES_IN_HOUR, SECONDS_IN_MINUTE, MS_IN_SEC } from './consts';

/**
* Milliseconds in day. Needs for calculating difference between dates in days.
*/
const MILLISECONDS_IN_DAY = HOURS_IN_DAY * MINUTES_IN_HOUR * SECONDS_IN_MINUTE * MS_IN_SEC;

/**
* Returns difference between now and payday in days
*
* Pay day is calculated by formula: paidUntil date or last charge date + 1 month
*
* @param date - last charge date
* @param paidUntil - paid until date
* @param isDebug - flag for debug purposes
*/
export function daysBeforePayday(date: Date, paidUntil: Date = null, isDebug = false): number {
const expectedPayDay = paidUntil ? new Date(paidUntil) : new Date(date);

if (isDebug) {
expectedPayDay.setDate(date.getDate() + 1);
} else if (!paidUntil) {
expectedPayDay.setMonth(date.getMonth() + 1);
}

const now = new Date().getTime();

return Math.floor((expectedPayDay.getTime() - now) / MILLISECONDS_IN_DAY);
}

/**
* Returns difference between payday and now in days
*
* Pay day is calculated by formula: paidUntil date or last charge date + 1 month
*
* @param date - last charge date
* @param paidUntil - paid until date
* @param isDebug - flag for debug purposes
*/
export function daysAfterPayday(date: Date, paidUntil: Date = null, isDebug = false): number {
const expectedPayDay = paidUntil ? new Date(paidUntil) : new Date(date);

if (isDebug) {
expectedPayDay.setDate(date.getDate() + 1);
} else if (!paidUntil) {
expectedPayDay.setMonth(date.getMonth() + 1);
}

const now = new Date().getTime();

return Math.floor((now - expectedPayDay.getTime()) / MILLISECONDS_IN_DAY);
}
Loading