搜索插件、代码仓与 APP 分类

插件详情v1.1.1

simple_moment

Moment.js 相对时间功能的简单实现。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
483
喜欢数
51
Pub 分数
120

安装插件

flutter pub add simple_moment

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个轻量级的 Flutter 包,提供类似 Moment.js 的基本功能,用于显示相对时间。它简化了时间差的格式化,例如 '2 分钟前' 或 '5 小时后',适用于多个平台。设计简洁易用,提供清晰的 API,可将时间戳转换为人类可读的相对字符串,且无繁重依赖。非常适合需要在用户界面中直观呈现时间的应用。

适合场景

  • 显示帖子时间戳
  • 显示消息发送时间
  • 更新实时活动指标
  • 增强用户通知
  • 跟踪事件截止日期

主要特点

  • 简单的相对时间格式化
  • 跨平台支持
  • 最小依赖足迹
  • 与 DateTime 对象的轻松集成
  • 无需外部库

适合对象

  • 具有实时更新的应用程序
  • 社交媒体界面
  • 聊天应用
  • 任务管理工具
  • 任何需要可读时间差的应用程序

使用建议

  • 仅限相对时间
  • 无日期解析或操作
  • 不支持本地化
  • 不适用于复杂的日期操作
  • 需要手动处理时区偏移

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

simple_moment Pub Build Status

A simple implementation of the Moment.js features for relative time in Dart.

Usage

A simple usage example:

import 'package:simple_moment/simple_moment.dart';

main() {
    var secondsToAdd = Duration(seconds: 10);
    var dateForComparison = DateTime.now().add(secondsToAdd);
    var moment = Moment.now();

    // should print "in a few seconds"
    print(moment.from(dateForComparison));
    
    Moment rawDate = Moment.parse("2020-07-03");
      // should print "2020-08-03 00:00:00.000
      print(rawDate.add(months: 1));
    
      // should print "2019-07-03 00:00:00.000
      print(rawDate.subtract(years: 1));
    
      //should print "03-07-2020 00:00"
      print(rawDate.format("dd-MM-yyyy HH:mm"));
}

Locales

Set the locale for all usages of Moment:
Moment.setLocaleGlobally(new LocaleDe());
Set the locale only for the current instance of Moment:
var moment = new Moment.now().locale(new LocaleDe());
Adding your own locale:

Just create a class that implements ILocaleData and assign that to your Moment instance or set it globally.

Overwriting existing locales:
class ShortLocaleEn extends LocaleEn {
  String get seconds => '%is';

  String get aMinute => '%im';
  String get minutes => '%im';

  String get anHour => '%ih';
  String get hours => '%ih';

  String get aDay => '%id';
  String get days => '%id';
}
Using intl to format dates
// create new Moment instance with german locale
var moment = Moment.now().locale(LocaleDe(), useInFormat: true);

// initialize intl
await initializeDateFormatting(moment.usedLocale.localeString);

// format and print the current month in german
var formattedString = moment.format("LLLL");
print(formattedString);

Features and bugs

Please file feature requests and bugs at the issue tracker.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目