@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
183 lines (182 loc) • 7.82 kB
JavaScript
/**
* CodeAnalizerComment: Updated 8 imports on 2024-09-21 23:07:24
* Update:: import { holidays } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;'
* Update:: import { msPerDay } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;'
* Update:: import { msPerHr } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;'
* Update:: import { msPerMin } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;'
* Update:: import { msPerMo } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;'
* Update:: import { msPerQ } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;'
* Update:: import { msPerWk } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;'
* Update:: import { msPerYr } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;'
*/
/***
* .d888b. .d88b. .d888b. .d88b. db .d888b. db j88D
* VP `8D .8P 88. VP `8D .8P 88. o88 VP `8D o88 j8~88
* odD' 88 d'88 odD' 88 d'88 88 odD' 88 j8' 88
* .88' 88 d' 88 .88' 88 d' 88 C8888D 88 .88' C8888D 88 V88888D
* j88. `88 d8' j88. `88 d8' 88 j88. 88 88
* 888888D `Y88P' 888888D `Y88P' VP 888888D VP VP
*
*
* d8888b. d888888b db db .d88b. d888888b d888888b d888888b db d88888b .d8888.
* 88 `8D `88' 88 88 .8P Y8. `~~88~~' `~~88~~' `88' 88 88' 88' YP
* 88oodD' 88 Y8 8P 88 88 88 88 88 88 88ooooo `8bo.
* 88~~~ 88 `8b d8' 88 88 88 88 88 88 88~~~~~ `Y8b.
* 88 .88. `8bd8' `8b d8' 88 88 .88. 88booo. 88. db 8D
* 88 Y888888P YP `Y88P' YP YP Y888888P Y88888P Y88888P `8888Y'
*
*
*/
/**
* Functions in this file
*
*
* import { getTimeDelta, getDayTimeToMinutes, getTimeSpan, getBestTimeDelta, getAge, createDeltaDateArrays } from '@mikezimm/npmfunctions/dist/Services/Time/deltas';
*
*/
import { msPerDay, msPerHr, msPerMin, msPerMo, msPerWk, msPerYr, } from './constants';
import { monthStr3 } from './monthLabels';
export function getTimeDelta(time1, time2, inWhat) {
let date = new Date(time1).getTime();
let now = new Date(time2).getTime();
let age = (now - date);
if (inWhat === 'months') {
age = age / msPerMo;
age = Math.round(age * 10) / 10; //2020-03-02: Added so that delta days is always whole number when in reality, 8 months out of the year there is an extra hour per day
}
else if (inWhat === 'weeks') {
age = age / msPerWk;
age = Math.round(age); //2020-03-02: Added so that delta days is always whole number when in reality, 8 months out of the year there is an extra hour per day
}
else if (inWhat === 'days') {
age = age / msPerDay;
age = Math.round(age); //2020-03-02: Added so that delta days is always whole number when in reality, 8 months out of the year there is an extra hour per day
}
else if (inWhat === 'hours') {
age = age / msPerHr;
}
else if (inWhat === 'minutes') {
age = age / msPerMin;
}
else if (inWhat === 'seconds') {
age = age / (1000);
}
else if (inWhat === 'ms') {
age = age;
}
else if (inWhat === 'best') {
}
return age;
}
export function getDayTimeToMinutes(startTime) {
let thisYear = new Date().getUTCFullYear();
let startYear = new Date(startTime).getUTCFullYear();
let replaceYear = (thisYear === startYear) ? "/" + thisYear : "";
let dateString = (new Date(startTime)).toLocaleDateString('short').replace(replaceYear, '');
let timeString = (new Date(startTime)).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
return [dateString, timeString].join(' ');
}
export function getTimeSpan(startTime, endTime) {
//console.log('getBestTimeDelta', startTime, endTime);
let theStartTime = getDayTimeToMinutes(startTime);
let forString = '- for';
let deltaString = getBestTimeDelta(startTime, endTime);
return [theStartTime, forString, deltaString].join(' ');
}
/**
*
* @param startTime for Time from now, make sure startTime is value
* @param endTime for Time from now, make sure endTime is now
* @returns
*/
export function getBestTimeDelta(startTime, endTime) {
let start = new Date(startTime).getTime();
let end = new Date(endTime).getTime();
let delta = end - start;
//console.log('getBestTimeDelta', startTime, endTime);
const isPast = delta > 0 ? true : false;
const ending = isPast === true ? 'ago' : 'from now';
delta = Math.abs(delta);
if ((delta / (1000)) < 60) {
return `${delta / (1000)} seconds ${ending}`;
}
else if ((delta / (msPerMin)) < 60) {
return `${(delta / (msPerMin)).toFixed(0)} minutes ${ending}`;
}
else if ((delta / (msPerHr)) < 24) {
return `${(delta / (msPerHr)).toFixed(0)} hours ${ending}`;
}
else if ((delta / (msPerDay)) < 7) {
return `${(delta / (msPerDay)).toFixed(0)} days ${ending}`;
}
else if ((delta / (msPerWk)) < 30) {
return `${(delta / (msPerWk)).toFixed(0)} weeks ${ending}`;
}
else if ((delta / (msPerMo)) < 24) {
return `${(delta / (msPerMo)).toFixed(0)} months ${ending}`;
}
else if ((delta / (msPerYr)) < 4) {
return `${(delta / (msPerYr)).toFixed(0)} years ${ending}`;
}
else {
return 'Infinity and Beyond!';
}
}
export function getAge(time, inWhat) {
let now = new Date().getTime();
let age = getTimeDelta(time, now, inWhat);
return age;
}
export function getAgeLabel(time, inWhat) {
let now = new Date().getTime();
let age = getTimeDelta(time, now, inWhat);
const result = `${age} ${inWhat} ${age > 0 ? 'ago' : 'from now'}`;
return result;
}
//
export function createDeltaDateArrays() {
let result = {
years: {
daysAgo: [],
daysAgoR: [],
daysAgoNull: [],
labelShort: [],
labelLong: [],
},
months: {
daysAgo: [],
daysAgoR: [],
daysAgoNull: [],
labelShort: [],
labelLong: [],
}
};
let rightNow = new Date();
let todayYear = rightNow.getFullYear();
let todayMonth = rightNow.getMonth(); //Zero Index
let todayDate = rightNow.getDate();
let todaysDate = new Date(todayYear, todayMonth, todayDate);
for (let y = todayYear; y > todayYear - 4; y--) {
for (let m = 11; m > -1; m--) {
let thisDate = new Date(y, m, 1);
let deltaDays = getTimeDelta(thisDate, todaysDate, 'days');
if (deltaDays > 0) {
result.months.daysAgo.push(deltaDays);
let roundedDays = Math.round(deltaDays);
result.months.daysAgoR.push(roundedDays);
result.months.labelShort.push(y.toString().substring(2) + '-' + monthStr3['en-us'][m]);
result.months.labelLong.push(y.toString() + '-' + monthStr3['en-us'][m]);
result.months.daysAgoNull[roundedDays] = null;
if (m === 0) {
result.years.daysAgo.push(deltaDays);
result.years.daysAgoR.push(roundedDays);
result.years.labelShort.push(y.toString().substring(2));
result.years.labelLong.push(y.toString());
result.years.daysAgoNull[roundedDays] = null;
}
}
}
}
return result;
}
//# sourceMappingURL=deltas.js.map