DateTime 객체를 인간이 읽기 쉬운 '몇 초 전', '며칠 전'과 같은 문자열로 변환하는 가벼운 Dart 패키지입니다. 앱에서 상대적 시간 표시를 간단하게 만들어 사용자 경험을 향상시키며, 자연스럽고 대화형 포맷으로 시간을 제공합니다. 여러 플랫폼을 지원하며 Flutter 프로젝트에 쉽게 통합할 수 있습니다.
사용 사례
소셜 미디어 앱에서 게시물 타임스탬프 표시
채팅 인터페이스에서 메시지 전송 시간 표시
상대 시간 레이블을 사용하여 활동 피드 업데이트
자연어 시간을 활용한 사용자 인터페이스 가독성 향상
실시간 애플리케이션에서 이벤트 지속 시간 추적
주요 기능
간단한 DateTime에서 문자열로의 변환
다국어 지원(로컬라이제이션을 통해)
크로스 플랫폼 호환성
필요한 보일러플레이트 최소화
사용자 정의 가능한 정밀도 및 출력 형식
적합한 대상
Flutter 개발자가 실시간 앱을 구축하고 있습니다
직관적인 시간 표시가 필요한 앱
UX에 중점을 둔 타임스탬프가 있는 콘텐츠를 가진 프로젝트
최소한의 날짜 형식 솔루션을 찾는 개발자
고려 사항
기본적으로 사용자 지정 로케일 형식을 지원하지 않습니다
상대적 시간 형식에 한정되며, 절대 날짜는 아님
시간대 차이를 수동으로 처리해야 합니다
원본 README
연결된 소스 저장소의 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.
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:
classMyCustomMessagesimplementsMessages{
@overrideStringprefixAgo() => '';
@overrideStringsuffixAgo() => 'ago';
// Implement the other methods here...
}
After (Version 2.0.0):
classMyCustomMessagesimplementsMessages{
@overrideStringprefixAgo() => '';
@overrideStringsuffixAgo() => 'ago';
@overrideStringjustNow(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.
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 pluginimport'package:get_time_ago/get_time_ago.dart';
// Pass DateTime object as argument in the methodvar _dateTime = DateTime.now().subtract(constDuration(minutes: 10)); // [DateTime] objectprint(GetTimeAgo.parse(_dateTime)); // 10 minutes ago// Formatting with localeprint(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 pluginimport'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] objectvar 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 pluginimport'package:get_time_ago/get_time_ago.dart';
@overridevoidinitState() {
super.initState();
GetTimeAgo.setDefaultLocale('fr'); // Sets the default locale to French
}
Setting Custom Locale & Messages
Implementing and Adding Custom Messages
classCustomMessagesimplementsMessages{
/// Prefix added before the time message.@overrideStringprefixAgo() => '';
/// Suffix added after the time message.@overrideStringsuffixAgo() => 'ago';
/// Message when the elapsed time is less than 15 seconds.@overrideStringjustNow(int seconds) => 'just now';
/// Message for when the elapsed time is less than a minute.@overrideStringsecsAgo(int seconds) => '$seconds seconds';
/// Message for when the elapsed time is about a minute.@overrideStringminAgo(int minutes) => 'a minute';
/// Message for when the elapsed time is in minutes.@overrideStringminsAgo(int minutes) => '$minutes minutes';
/// Message for when the elapsed time is about an hour.@overrideStringhourAgo(int minutes) => 'an hour';
/// Message for when the elapsed time is in hours.@overrideStringhoursAgo(int hours) => '$hours hours';
/// Message for when the elapsed time is about a day.@overrideStringdayAgo(int hours) => 'a day';
/// Message for when the elapsed time is in days.@overrideStringdaysAgo(int days) => '$days days';
/// Word separator to be used when joining the parts of the message.@overrideStringwordSeparator() => ' ';
}
Overriding en Locale Messages with Custom Messages
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 file.
License
This project is licensed under the MIT License - see the LICENSE file for details.
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!