Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev to Main Sync #603

Merged
merged 1 commit into from
Jul 15, 2024
Merged
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
56 changes: 23 additions & 33 deletions app/helpers/convertDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,47 @@ const timeDifference = (timestamp, timeNow) => {
const mins = calc(60, timeInSec);
const hours = calc(60, mins);
const days = calc(24, hours);
const weeks = calc(7, days);
const months = calc(30, days);
const years = calc(12, months);
let result = years;
let cycle = 'year';
const months = Math.floor(days / 30);
const years = Math.floor(months / 12);

console.log({ timeInSec, mins, hours, days, months, years }); // Debug log

if (timeInSec < 1) {
return 'just now';
return { result: '', cycle: 'just now' };
}

if (years > 0) {
result = years;
cycle = 'year';
return { result: years, cycle: 'year' };
} else if (months > 0) {
result = months;
cycle = 'month';
} else if (weeks > 0) {
result = weeks;
cycle = 'week';
return { result: months, cycle: 'month' };
} else if (days > 0) {
result = days;
cycle = 'day';
return { result: days, cycle: 'day' };
} else if (hours > 0) {
result = hours;
cycle = 'hour';
return { result: hours, cycle: 'hour' };
} else if (mins > 0) {
result = mins;
cycle = 'minute';
return { result: mins, cycle: 'minute' };
} else {
result = '';
cycle = 'few seconds';
return { result: '', cycle: 'few seconds' };
}
return { result, cycle };
};

function convertDate([timestamp], { end_date, timeNow = Date.now() }) {
if (!timestamp) return 'TBD';
if (end_date == 1 && timestamp * 1000 < timeNow) {
const time_value = timeDifference(timestamp, timeNow);
return `${time_value.result} ${time_value.cycle}${
time_value.result > 1 ? 's' : ''
} ago`;
}
const time_value = timeDifference(timestamp, timeNow);
if (timestamp * 1000 < timeNow)
return `${time_value.result} ${time_value.cycle}${
time_value.result > 1 ? 's ago' : ' ago'
}`;
return `in ${time_value.result} ${time_value.cycle}${

let timeString = `${time_value.result} ${time_value.cycle}${
time_value.result > 1 ? 's' : ''
}`;

if (end_date == 1 && timestamp * 1000 < timeNow) {
return `${timeString} ago`;
}

if (timestamp * 1000 < timeNow) {
return `${timeString} ago`;
}

return `in ${timeString}`;
}

export default helper(convertDate);
Loading