https://github.com/nixrajput/get_time_ago
A Dart package that converts and formats `DateTime` objects into human-readable 'time ago' strings, such as '20 seconds ago', 'a minute ago', or '7 hours ago'.
https://github.com/nixrajput/get_time_ago
collaborate dart datetime datetime-format datetime-to-string flutter flutter-packages flutterdev get-time-ago get-time-ago-flutter nixlab nixlab-packages nixrajput nixrajput-github packages pub timago time-ago time-ago-flutter
Last synced: 8 months ago
JSON representation
A Dart package that converts and formats `DateTime` objects into human-readable 'time ago' strings, such as '20 seconds ago', 'a minute ago', or '7 hours ago'.
- Host: GitHub
- URL: https://github.com/nixrajput/get_time_ago
- Owner: nixrajput
- License: mit
- Created: 2020-09-10T22:05:08.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-03-11T08:22:13.000Z (10 months ago)
- Last Synced: 2025-05-07T15:10:05.150Z (8 months ago)
- Topics: collaborate, dart, datetime, datetime-format, datetime-to-string, flutter, flutter-packages, flutterdev, get-time-ago, get-time-ago-flutter, nixlab, nixlab-packages, nixrajput, nixrajput-github, packages, pub, timago, time-ago, time-ago-flutter
- Language: Dart
- Homepage: https://pub.dev/packages/get_time_ago
- Size: 6.79 MB
- Stars: 25
- Watchers: 2
- Forks: 27
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# get_time_ago
A Dart package that converts and formats `DateTime` objects into human-readable 'time ago' strings, such as '20 seconds ago', 'a minute ago', or '7 hours ago'. This package provides an easy way to display relative time differences in your Flutter or Dart applications. It supports various time units (seconds, minutes, hours, days, etc.) and automatically adjusts the format based on how recent the event occurred. Ideal for displaying timestamps in news feeds, social media posts, or chat messages.
[][pub]
[][repo]
[][repo]
[][repo]
[][repo]
[][repo]
[][repo]
[][repo]
[][repo]
[][repo]
[][releases]
[][releases]
[][repo]
[][issues]
[][pulls]
[][license]
## Table of Contents
- [get\_time\_ago](#get_time_ago)
- [Table of Contents](#table-of-contents)
- [Breaking Changes for the Version ^2.0.0](#breaking-changes-for-the-version-200)
- [1. Added `justNow` Method in `Messages` Interface](#1-added-justnow-method-in-messages-interface)
- [Impact on Custom Implementations:](#impact-on-custom-implementations)
- [Example of Custom Locale Update:](#example-of-custom-locale-update)
- [Demo](#demo)
- [Installation](#installation)
- [Usage](#usage)
- [Formatting String as `get_time_ago`](#formatting-string-as-get_time_ago)
- [Setting default locale](#setting-default-locale)
- [Setting Custom Locale \& Messages](#setting-custom-locale--messages)
- [Supported Languages](#supported-languages)
- [Contributing](#contributing)
- [License](#license)
- [Contributors](#contributors)
- [Support My Work](#support-my-work)
- [Connect With Me](#connect-with-me)
- [Activities](#activities)
## Breaking Changes for the Version ^2.0.0
### 1. Added `justNow` Method in `Messages` Interface
In version 2.0.0, a new method `justNow(int seconds)` has been added to the `Messages` interface. This method is responsible for providing a custom message when the elapsed time is less than 15 seconds.
### Impact on Custom Implementations:
If you have implemented any custom locales by extending the `Messages` interface, you will need to update your implementation to include the new `justNow` method. Failing to implement this method will result in compilation errors.
### Example of Custom Locale Update:
Before:
```dart
class MyCustomMessages implements Messages {
@override
String prefixAgo() => '';
@override
String suffixAgo() => 'ago';
// Implement the other methods here...
}
```
After (Version 2.0.0):
```dart
class MyCustomMessages implements Messages {
@override
String prefixAgo() => '';
@override
String suffixAgo() => 'ago';
@override
String justNow(int seconds) => 'just now'; // New method
// Implement the other methods here...
}
```
This section explains the breaking change, the impact it has on custom implementations, and provides an example of how to update existing code to comply with the new changes in version ^2.0.0.
## Demo
Click here to experience the demo in a Web App
## Installation
Add `get_time_ago` as a dependency in your `pubspec.yaml` file.
```yaml
dependencies:
get_time_ago: ^latest_version
```
## Usage
Format any `DateTime` object into `get_time_ago` format by following steps:
```dart
// Import the plugin
import 'package:get_time_ago/get_time_ago.dart';
// Pass DateTime object as argument in the method
var _dateTime = DateTime.now().subtract(const Duration(minutes: 10)); // [DateTime] object
print(GetTimeAgo.parse(_dateTime)); // 10 minutes ago
// Formatting with locale
print(GetTimeAgo.parse(_dateTime, locale:'es')); // hace 10 minutos
```
### Formatting String as `get_time_ago`
If you have saved a `DateTime` object as a String into a variable, database or cloud, then you have
to first convert the String into `DateTime` object and then pass it as argument in `parse` method
of `get_time_ago` plugin to format it into `get_time_ago` format by following steps:
```dart
// Import the plugin
import 'package:get_time_ago/get_time_ago.dart';
var _timestamp = '2021-05-10 05:21:37.712498'; // [DateTime] formatted as String.
var _convertedTimestamp = DateTime.parse(_timestamp); // Converting into [DateTime] object
var result = GetTimeAgo.parse(_convertedTimestamp);
print(result);
```
### Setting default locale
If you want to change your default `locale`, then call `setDefaultLocale` method and pass
the `locale` code as the argument.
```dart
// Import the plugin
import 'package:get_time_ago/get_time_ago.dart';
@override
void initState() {
super.initState();
GetTimeAgo.setDefaultLocale('fr'); // Sets the default locale to French
}
```
### Setting Custom Locale & Messages
Implementing and Adding Custom Messages
```dart
class CustomMessages implements Messages {
/// Prefix added before the time message.
@override
String prefixAgo() => '';
/// Suffix added after the time message.
@override
String suffixAgo() => 'ago';
/// Message when the elapsed time is less than 15 seconds.
@override
String justNow(int seconds) => 'just now';
/// Message for when the elapsed time is less than a minute.
@override
String secsAgo(int seconds) => '$seconds seconds';
/// Message for when the elapsed time is about a minute.
@override
String minAgo(int minutes) => 'a minute';
/// Message for when the elapsed time is in minutes.
@override
String minsAgo(int minutes) => '$minutes minutes';
/// Message for when the elapsed time is about an hour.
@override
String hourAgo(int minutes) => 'an hour';
/// Message for when the elapsed time is in hours.
@override
String hoursAgo(int hours) => '$hours hours';
/// Message for when the elapsed time is about a day.
@override
String dayAgo(int hours) => 'a day';
/// Message for when the elapsed time is in days.
@override
String daysAgo(int days) => '$days days';
/// Word separator to be used when joining the parts of the message.
@override
String wordSeparator() => ' ';
}
```
Overriding `en` Locale Messages with Custom Messages
```dart
GetTimeAgo.setCustomLocaleMessages('en', CustomMessages());
```
## Supported Languages
- Arabic
- English
- Spanish
- Persian (Farsi)
- French
- Hindi
- Portuguese (Brazil)
- Portuguese (Brazil alternate)
- Simplified Chinese
- Traditional Chinese
- Japanese
- Occitan
- Korean
- German
- Indonesian
- Turkish
- Urdu
- Vietnamese
- Romanian
- Dutch
- Nepali
- Italian
- Open to accept PR for adding more languages
## Contributing
If you would like to contribute to this project, feel free to fork the repository, make your changes, and submit a pull request. Please follow the guidelines in the [CONTRIBUTING.md](CONTRIBUTING.md) file.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Contributors
Made with [contrib.rocks](https://contrib.rocks).
## Support My Work
Your support helps me dedicate more time to developing high-quality, impactful projects in the open-source community. Sponsor me, and together, let’s bring even more innovation to life!
[](https://github.com/sponsors/nixrajput)
[](https://ko-fi.com/nixrajput)
[](https://www.buymeacoffee.com/nixrajput)
## Connect With Me
[][github]
[][linkedin]
[][instagram]
[][twitter]
[][telegram]
[][gmail]
## Activities

[pub]: https://pub.dev/packages/get_time_ago
[github]: https://github.com/nixrajput
[telegram]: https://telegram.me/nixrajput
[twitter]: https://twitter.com/nixrajput07
[instagram]: https://instagram.com/nixrajput
[linkedin]: https://linkedin.com/in/nixrajput
[gmail]: mailto:nkr.nikhil.nkr@gmail.com
[releases]: https://github.com/nixrajput/get_time_ago/releases
[repo]: https://github.com/nixrajput/get_time_ago
[issues]: https://github.com/nixrajput/get_time_ago/issues
[license]: https://github.com/nixrajput/get_time_ago/blob/master/LICENSE
[pulls]: https://github.com/nixrajput/get_time_ago/pulls