apphouse
Version:
Component library for React that uses observable state management and theme-able components.
39 lines (36 loc) • 822 B
text/typescript
const MAX_DAYS = 30;
const TOTAL_DAYS_IN_YEAR = 360;
/**
* Get days, months, years
* @param value time usually created by getTime()
* @returns
*/
export const getYMDFromDays = (value?: number) => {
if (!value) {
return {
days: 0,
months: 0,
years: 0
};
}
const years = Math.floor(value / TOTAL_DAYS_IN_YEAR);
const remainingMonths = value % TOTAL_DAYS_IN_YEAR;
let months = 0;
let days = 0;
if (years > 0) {
if (remainingMonths > 0) {
months = Math.floor(remainingMonths / MAX_DAYS);
days = remainingMonths % MAX_DAYS;
}
} else if (remainingMonths / MAX_DAYS > 0) {
months = Math.floor(remainingMonths / MAX_DAYS);
days = Math.floor(remainingMonths % MAX_DAYS);
} else {
days = value;
}
return {
days,
months,
years
};
};