@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
107 lines (106 loc) • 5.02 kB
JavaScript
/**
* CodeAnalizerComment: Updated 3 imports on 2024-09-22 02:56:43
* Update:: import { getAge } to '@mikezimm/fps-core-v7/lib/logic/Time/deltas;'
* Update:: import { getAgeLabel } to '@mikezimm/fps-core-v7/lib/logic/Time/deltas;'
* Update:: import { getBestTimeDelta } to '@mikezimm/fps-core-v7/lib/logic/Time/deltas;'
*/
import { getAge, getBestTimeDelta } from '../../Time/deltas';
export function convertUTCTime(trimmedItem, rightSide) {
if (typeof trimmedItem !== 'string') {
return 'No date';
}
const rightNow = new Date(); // Current Date
const thisTime = new Date(trimmedItem);
if (!thisTime) {
return 'No date';
}
const year = thisTime.getFullYear();
const month = (thisTime.getMonth() + 1).toString();
const date = thisTime.getDate();
const hour24 = thisTime.getHours();
const hour12 = thisTime.getHours() - 12;
const mins = thisTime.getMinutes().toString();
const secs = thisTime.getSeconds().toFixed();
const quarter = `Q${Math.floor(thisTime.getMonth() / 3 + 1)}`;
let hourStamp = '';
let AMStamp = '';
const minStamp = mins.length === 1 ? `0${mins}` : mins;
const secStamp = secs.length === 1 ? `0${secs}` : secs;
const monthStamp = month.length === 1 ? `0${month}` : month;
let needsAM = rightSide.indexOf('AM') > -1 ? true : false;
let isAM = hour12 < 0 ? true : false;
if (needsAM === true) {
hourStamp = hour12 >= 0 ? hour12.toString() : hour24.toString();
AMStamp = isAM === true ? ' AM' : ' PM';
if (hourStamp.length === 1) {
hourStamp = `0${hourStamp}`;
}
}
else {
hourStamp = hour24.toString();
if (hourStamp.length === 1) {
hourStamp = `0${hourStamp}`;
}
}
const theDate = date > 9 ? `${date}` : `0${date}`;
let result = '';
switch (rightSide) {
case 'YYYY-MM':
result = `${year}-${monthStamp}`;
break;
case 'YYYY-MM-DD':
result = `${year}-${monthStamp}-${theDate}`;
break;
case 'MM-DD': // https://github.com/mikezimm/drilldown7/issues/370
result = `${monthStamp}-${theDate}`;
break;
case '????-MM-DD': // https://github.com/mikezimm/drilldown7/issues/370 Conditionally sets year only if date is outside of current year. Else leaves it off
const prefix = rightNow.getFullYear() === year ? '' : `${year}-`;
result = `${prefix}${monthStamp}-${theDate}`;
break;
case 'YYYY-MM-DD-HH:mm':
case 'YYYY-MM-DD-HH:mm_AM':
result = `${year}-${monthStamp}-${theDate} ${hourStamp}:${minStamp}${AMStamp}`;
break;
case 'HH:mm':
case 'HH:mm_AM':
result = `${hourStamp}:${minStamp}${AMStamp}`;
break;
case 'HH:mm:ss':
case 'HH:mm:ss_AM':
result = `${hourStamp}:${minStamp}:${secStamp}${AMStamp}`;
break;
case 'Q1-YY':
result = `${quarter}-${year.toString().slice(-2)}`;
break;
case 'YY-Q1':
result = `${year.toString().slice(-2)}-${quarter}`;
break;
case 'YYYY-Q1':
result = `${year.toString()}-${quarter}`;
break;
case 'TimeFromNow':
let now = new Date().toUTCString();
const age0 = getBestTimeDelta(trimmedItem, now); // Will be + if in the past, - if in the future
result = `${age0}`;
break;
case 'DaysAgo':
const age1 = getAge(trimmedItem, 'days'); // Will be + if in the past, - if in the future
result = age1 === 0 ? `within a day` : age1 > 0 ? `${age1} days ago` : `in future`; // Returns empty string if date is in the future. Use getBestTimeDelta to get label either way
break;
case 'WeeksAgo':
const age2 = getAge(trimmedItem, 'weeks'); // Will be + if in the past, - if in the future
result = age2 === 0 ? `within a week` : age2 > 0 ? `${age2} wks ago` : `in future`; // Returns empty string if date is in the future. Use getBestTimeDelta to get label either way
break;
case 'DaysFromNow':
const age3 = getAge(trimmedItem, 'days'); // Will be + if in the past, - if in the future
result = age3 === 0 ? `within a day` : age3 < 0 ? `${age3} days from now` : `in past`; // Returns empty string if date is in the past. Use getBestTimeDelta to get label either way
break;
case 'WeeksFromNow':
const age4 = getAge(trimmedItem, 'weeks'); // Will be + if in the past, - if in the past. Use getBestTimeDelta to get label either way
result = age4 === 0 ? `within a week` : age4 < 0 ? `${age4} wks from now` : `in past`;
break;
}
return result;
}
//# sourceMappingURL=convertUTCTime.js.map