smarttime/ts/smarttime.units.ts

57 lines
1.4 KiB
TypeScript
Raw Normal View History

2017-08-16 14:24:56 +00:00
export let units = {
years: (timesArg = 1): number => {
2018-03-11 15:44:32 +00:00
return timesArg * 3.154e10;
2017-08-16 14:24:56 +00:00
},
months: (timesArg = 1): number => {
2018-03-11 15:44:32 +00:00
return timesArg * 2.628e9;
2017-08-16 14:24:56 +00:00
},
weeks: (timesArg = 1) => {
2018-03-11 15:44:32 +00:00
return timesArg * 6.048e8;
2017-08-16 14:24:56 +00:00
},
days: (timesArg = 1) => {
2018-03-11 15:44:32 +00:00
return timesArg * 8.64e7;
2017-08-16 14:24:56 +00:00
},
hours: (timesArg = 1) => {
2018-03-11 15:44:32 +00:00
return timesArg * 3.6e6;
2017-08-16 14:24:56 +00:00
},
minutes: (timesArg = 1) => {
2018-03-11 15:44:32 +00:00
return timesArg * 60000;
2017-08-16 14:24:56 +00:00
}
2018-03-11 15:44:32 +00:00
};
2017-08-16 14:24:56 +00:00
export interface IUnitCombinationArg {
2018-03-11 15:44:32 +00:00
years?: number;
months?: number;
weeks?: number;
days?: number;
hours?: number;
minutes?: number;
2017-08-16 14:24:56 +00:00
}
export let getMilliSecondsFromUnits = (combinationArg: IUnitCombinationArg) => {
2018-03-11 15:44:32 +00:00
let timeInMilliseconds = 0;
let addMilliSeconds = milliSecondsArg => {
timeInMilliseconds = timeInMilliseconds + milliSecondsArg;
};
if (combinationArg.years) {
addMilliSeconds(units.years(combinationArg.years));
}
if (combinationArg.months) {
addMilliSeconds(units.months(combinationArg.months));
}
if (combinationArg.weeks) {
addMilliSeconds(units.weeks(combinationArg.weeks));
}
if (combinationArg.days) {
addMilliSeconds(units.days(combinationArg.days));
}
if (combinationArg.hours) {
addMilliSeconds(units.hours(combinationArg.hours));
}
if (combinationArg.minutes) {
addMilliSeconds(units.minutes(combinationArg.minutes));
2017-08-16 14:24:56 +00:00
}
2018-03-11 15:44:32 +00:00
return timeInMilliseconds;
};