搜索分类、插件与代码仓

插件详情v2.3.0

calendar_time

MomentJS 的日历时间实现,以及许多其他辅助方法。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
124
喜欢数
27
Pub 分数
130

安装插件

flutter pub add calendar_time

插件介绍

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

一个 Flutter/Dart 包,实现了 MomentJS 的日历时间格式化功能,提供直观的相对时间显示,例如“2小时后”或“2天前”。它包含用于日期操作和格式化的额外辅助方法,使在面向用户的应用程序中处理时间戳更加轻松。专为跨平台使用设计,支持 Android、iOS、Web、macOS、Windows 和 Linux。该包简化了以人类可读格式呈现时间差,增强了需要动态时间显示的应用程序的用户体验。

适合场景

  • 在聊天应用中显示相对时间戳
  • 在社交动态中显示帖子或活动的年龄
  • 通知中格式化时间差
  • 通过可读的时间标签提升用户体验
  • 简化任务管理应用中的日期逻辑

主要特点

  • MomentJS 风格的日历时间格式化
  • 跨平台兼容性
  • 日期操作的辅助方法
  • 可读的时间表达式
  • 通过 pub add 命令轻松集成

适合对象

  • 需要相对时间显示的应用程序
  • 熟悉 MomentJS 的开发者
  • 需要一致时间格式化的项目
  • 具有实时更新的UI密集型应用
  • 注重开发者生产力的团队

使用建议

  • 需要理解相对时间概念
  • 可能需要为非英文区域设置本地化
  • 仅限时间格式化——不支持完整日期解析
  • 依赖于 Dart 的核心 DateTime 类
  • 不适合复杂的日期计算

原始 README

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

calendar_time

Implementation of MomentJS's calendarTime function and some other helpful utilities.

I expect this project will get replaced with another more ambitious project in the future, but this has come in very handy. Extensions and pull requests welcome.

Usage

toHuman

Converting a DateTime to a human readable string

import 'package:calendar_time/calendar_time.dart';

main(){
  CalendarTime(DateTime.now()).toHuman; //Today at {current time}
}

Example output:

  • Today at {current time}
  • Tomorrow at {time}
  • Yesterday at {time}
  • {day} at {time}
  • Last {day} at {time}
  • date time (outside of the last or upcoming few weeks)

Honestly just check the code, but it is in line with Moment's logic.

You can also call .toHumanMultiLine that splits the output around at to have date and time on different lines.

format

Formats to current timezone

import 'package:calendar_time/calendar_time.dart';

main(){
  CalendarTime(DateTime.now()).format("yyyy-MM-dd HH:mm");
}

See https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html for options.

isToday

Return if the supplied DateTime is today

import 'package:calendar_time/calendar_time.dart';

main(){
  final calendarTime = CalendarTime(DateTime.now());
  calendarTime.isToday; //true
}
isTomorrow

Return if the supplied DateTime is tomorrow

import 'package:calendar_time/calendar_time.dart';

main(){
  final calendarTime = CalendarTime(DateTime.now());
  calendarTime.isTomorrow; //false
}
isNextWeek

Return if the supplied DateTime is next week

import 'package:calendar_time/calendar_time.dart';

main(){
  final calendarTime = CalendarTime(DateTime.now());
  calendarTime.isNextWeek; //false
}
isYesterday

Return if the supplied DateTime is yesterday

import 'package:calendar_time/calendar_time.dart';

main(){
  final calendarTime = CalendarTime(DateTime.now());
  calendarTime.isYesterday; //false
}
isLastweek

Return if the supplied DateTime is last week

import 'package:calendar_time/calendar_time.dart';

main(){
  final calendarTime = CalendarTime(DateTime.now());
  calendarTime.isLastWeek; //false
}
startOfToday

Return the beginning of today - not to be confused with startOfDay

import 'package:calendar_time/calendar_time.dart';

main(){
  final calendarTime = CalendarTime(DateTime.now());
  calendarTime.startOfToday; // DateTime
}
startOfDay

Return the beginning of the day of the CalendarTime obejct.

import 'package:calendar_time/calendar_time.dart';

main(){
  final calendarTime = CalendarTime(DateTime(2020,1,1));
  calendarTime.startOfDay; // 2020/01/01 0:0:0am
}
startOfYesterday

Return the beginning of yesterday

import 'package:calendar_time/calendar_time.dart';

main(){
  final calendarTime = CalendarTime(DateTime.now());
  calendarTime.startOfYesterday; // DateTime start of yesterday
}
startOfLastWeek

Return the beginning of lastWeek

import 'package:calendar_time/calendar_time.dart';

main(){
  final calendarTime = CalendarTime(DateTime.now());
  calendarTime.startOfLastWeek; // DateTime start of yesterday
}
endOfToday

Return the end of today (23:59:59:999:999) - not to be confused with endOfDay

import 'package:calendar_time/calendar_time.dart';

main(){
  final calendarTime = CalendarTime(DateTime.now());
  calendarTime.endOfToday; // DateTime
}
endOfDay

Return the end of the day of the CalendarTime object (23:59:59:999:999)

import 'package:calendar_time/calendar_time.dart';

main(){
  final calendarTime = CalendarTime(DateTime(2020, 1,1));
  calendarTime.endOfToday; // 2020/01/01 23:59:59:999:999
}
endOfTomorrow

Return the end of tomorrow

import 'package:calendar_time/calendar_time.dart';

main(){
  final calendarTime = CalendarTime(DateTime.now());
  calendarTime.endOfTomorrow; // DateTime
}
endOfNextWeek

Return the end of next week

import 'package:calendar_time/calendar_time.dart';

main(){
  final calendarTime = CalendarTime(DateTime.now());
  calendarTime.endOfNextWeek; // DateTime
}

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。