Skip to content

A Flutter package to convert and format DateTime object into get_time_ago format to get String like 10 seconds ago, a minute ago, 7 hours ago, etc.

License

Notifications You must be signed in to change notification settings

silviuilas/get-time-ago

 
 

Repository files navigation

get_time_ago

A Flutter package to convert and format DateTime object into get_time_ago format to get
String like 10 seconds ago, a minute ago, 7 hours ago, etc.

pub package Stars Forks Watchers Contributors CI

GitHub release (latest by date) GitHub last commit GitHub issues GitHub pull requests GitHub Licence

Installation

Add get_time_ago as a dependency in your pubspec.yaml file.

dependencies:
  get_time_ago: latest_version

Usage

Format any DateTime object into get_time_ago format by following steps:

// 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:

// 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.

// 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

class CustomMessages implements Messages {
  @override
  String prefixAgo() => '';

  @override
  String suffixAgo() => 'ago';

  @override
  String secsAgo(int seconds) => '$seconds seconds';

  @override
  String minAgo(int minutes) => 'a minute';

  @override
  String minsAgo(int minutes) => '$minutes minutes';

  @override
  String hourAgo(int minutes) => 'an hour';

  @override
  String hoursAgo(int hours) => '$hours hours';

  @override
  String dayAgo(int hours) => 'a day';

  @override
  String daysAgo(int days) => '$days days';

  @override
  String wordSeparator() => ' ';
}

Overriding en Locale Messages with Custom Messages

GetTimeAgo.setCustomLocaleMessages('en', CustomMessages());

Demo

View Demo

Activities

Alt

Connect With Me

Instagram: nixrajput Linkedin: nixrajput GitHub: nixrajput Twitter: nixrajput07 Facebook: nixrajput07

About

A Flutter package to convert and format DateTime object into get_time_ago format to get String like 10 seconds ago, a minute ago, 7 hours ago, etc.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 86.8%
  • HTML 11.3%
  • Swift 1.1%
  • Other 0.8%