Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
feature: ✨ add util - get-elapsed-time (#111)
Browse files Browse the repository at this point in the history
* feature: ✨ add util - get-past-time

* refactor: ♻️ rename get-past-time => get-elapsed-time. And add docs
  • Loading branch information
velenyx committed Sep 25, 2023
1 parent de7fec8 commit db4e68c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions client/src/shared/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './react-query';
export * from './ts-particles';
export * from './hooks';
export * from './utils';
34 changes: 34 additions & 0 deletions client/src/shared/lib/utils/get-elapsed-time/get-elapsed-time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Calculates and formats the time elapsed from a specified time to the current time.
*
* @param {string | number | Date} time - The past time from which the elapsed time is calculated. This can be a string representing a date, a number representing the milliseconds since January 1, 1970, 00:00:00 UTC, or a Date object.
* @returns {string} A string representing the elapsed time in a human-readable format. The string contains the largest non-zero time unit followed by an abbreviation for that unit and " ago". For example, "1h ago" for one hour ago or "2d ago" for two days ago.
*
* @example
* // Assuming the current date and time is "2023-09-25T12:00:00.000Z"
* // returns "1d ago"
* getElapsedTime(new Date("2023-09-24T12:00:00.000Z"));
*/
export const getElapsedTime = (time: string | number | Date) => {
const datePast = new Date(time);
const dateNow = new Date();
const timeDiff = dateNow.getTime() - datePast.getTime();

const seconds = Math.floor(timeDiff / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
const months =
(dateNow.getFullYear() - datePast.getFullYear()) * 12 +
dateNow.getMonth() -
datePast.getMonth();
const years = Math.floor(months / 12);

if (years > 0) return `${years}y ago`;
if (months > 0) return `${months}mo ago`;
if (days > 0) return `${days}d ago`;
if (hours > 0) return `${hours}h ago`;
if (minutes > 0) return `${minutes}m ago`;

return `${seconds}s ago`;
};
1 change: 1 addition & 0 deletions client/src/shared/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getElapsedTime } from './get-elapsed-time/get-elapsed-time';

2 comments on commit db4e68c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for teameights ready!

✅ Preview
https://teameights-5awparpud-exortme1ster.vercel.app

Built with commit db4e68c.
This pull request is being automatically deployed with vercel-action

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for teameights-storybook ready!

✅ Preview
https://teameights-storybook-nkqg3rbk2-exortme1ster.vercel.app

Built with commit db4e68c.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.