A lightweight TypeScript library to convert MongoDB ISODate timestamps into human-readable "time ago" strings.
- Converts MongoDB
ISODateor standard timestamps to "time ago" format. - Fully written in TypeScript for type safety.
- Supports CommonJS and ES Modules.
- Lightweight and easy to integrate.
Install the package using NPM:
npm install utc-To-TimeAgoOr using Yarn:
yarn add utc-To-TimeAgoUsing ES Modules:
import { utcToTimeAgo } from "utc-To-TimeAgo";Using CommonJS:
const { utcToTimeAgo } = require("utc-To-TimeAgo");import { utcToTimeAgo } from "utc-To-TimeAgo";
const timestamp = "2024-11-20T18:30:00Z"; // MongoDB ISODate format
const result = utcToTimeAgo(timestamp);
console.log(result); // Example Output: "2 months ago"You can also pass a JavaScript Date object:
const date = new Date("2024-11-20T18:30:00Z");
console.log(utcToTimeAgo(date)); // Example Output: "2 months ago"The library calculates the difference between the current time and the provided timestamp. It then returns a human-readable string, such as:
just now5 seconds ago2 days ago1 year ago
timestamp(required): A string in ISO 8601 format (e.g.,"2024-11-20T18:30:00Z") or a JavaScriptDateobject.
- A
stringrepresenting the "time ago" format, such as"2 hours ago"or"1 year ago".
utcToTimeAgo("2024-11-20T18:30:00Z"); // "2 months ago"
utcToTimeAgo(new Date("2024-11-20T18:30:00Z")); // "2 months ago"Make sure you have Node.js and NPM installed.
git clone https://github.com/Shaileshkale17/utc-To-TimeAgo.git
cd utc-To-TimeAgonpm installTo compile the TypeScript code into JavaScript, run:
npm run buildThe compiled files will be available in the dist directory.
You can test the library locally by linking it to another project:
npm linkThen, in the other project:
npm link utc-To-TimeAgoContributions are welcome! If you'd like to contribute:
- Fork the repository.
- Create a new branch:
git checkout -b feature-name. - Commit your changes:
git commit -m 'Add feature'. - Push to the branch:
git push origin feature-name. - Submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
If you encounter issues or have suggestions, feel free to open an issue on GitHub.
- Inspired by real-world requirements for MongoDB timestamps.
- Built with ❤️ by Shailesh kale.