DateTime - 基础时间类
// 创建 DateTime 对象 DateTime now = DateTime.now(); // 当前时间 DateTime specific = DateTime(2024, 1, 15); // 指定日期 (年,月,日) DateTime detailed = DateTime(2024, 1, 15, 10, 30); // 指定日期时间 (年,月,日,时,分) DateTime utc = DateTime.utc(2024, 1, 15); // UTC 时间 DateTime parsed = DateTime.parse("2024-01-15 10:30:45"); // 解析字符串 // 常用属性 print(now.year); // 年: 2024 print(now.month); // 月: 1-12 print(now.day); // 日: 1-31 print(now.hour); // 时: 0-23 print(now.minute); // 分: 0-59 print(now.second); // 秒: 0-59 print(now.millisecond);// 毫秒 print(now.microsecond);// 微秒 print(now.weekday); // 星期几: 1(星期一)-7(星期日) print(now.millisecondsSinceEpoch); // 时间戳(毫秒) print(now.microsecondsSinceEpoch); // 时间戳(微秒) // 时区相关 print(now.timeZoneName); // 时区名: "CST" print(now.timeZoneOffset); // 时区偏移: 8:00:00.000000 print(now.isUtc); // 是否是UTC时间Duration - 时间段类
// 创建 Duration Duration duration1 = Duration(days: 1, hours: 2, minutes: 30); Duration duration2 = Duration(seconds: 90); // 1分30秒 Duration duration3 = Duration(milliseconds: 1500); // 1.5秒 // 常用属性 Duration d = Duration(hours: 2, minutes: 30); print(d.inDays); // 0 (不足1天) print(d.inHours); // 2 print(d.inMinutes); // 150 print(d.inSeconds); // 9000 print(d.inMilliseconds);// 9000000 // 运算 Duration sum = duration1 + duration2; Duration diff = duration1 - duration2; Duration multiplied = duration1 * 2; Duration divided = duration1 ~/ 2; bool isGreater = duration1 > duration2;基本时间格式化
import 'package:intl/intl.dart'; DateTime now = DateTime.now(); //.format() 用于将DateTime对象格式化为指定格式的字符串 // 日期格式化 DateFormat dateFormat = DateFormat("yyyy-MM-dd"); String formattedDate = dateFormat.format(now); // "2024-01-15" // 时间格式化 DateFormat timeFormat = DateFormat("HH:mm:ss"); String formattedTime = timeFormat.format(now); // "10:30:45" // 日期时间格式化 DateFormat dateTimeFormat = DateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = dateTimeFormat.format(now); // "2024-01-15 10:30:45" // 本地化格式化 DateFormat localeFormat = DateFormat.yMMMMd('zh_CN'); String localized = localeFormat.format(now); // "2024年1月15日"常用格式模板
// 日期部分 yyyy // 4位年份: 2024 yy // 2位年份: 24 MMMM // 完整月份: January MMM // 缩写月份: Jan MM // 2位月份: 01 M // 月份: 1 dd // 2位日期: 15 d // 日期: 15 // 时间部分 HH // 24小时制: 14 hh // 12小时制: 02 mm // 分钟: 30 ss // 秒: 45 a // AM/PM: AM // 星期 EEEE // 完整星期: Monday EEE // 缩写星期: Mon // 组合示例 DateFormat("yyyy年MM月dd日 EEEE"); // 2024年1月15日 星期一 DateFormat("MM/dd/yyyy hh:mm a"); // 01/15/2024 02:30 PM DateFormat("yyyy-MM-dd'T'HH:mm:ss");// 2024-01-15T14:30:45预定于格式
// 日期格式 DateFormat.yMd(); // 2024/1/15 DateFormat.yMMMMd(); // January 15, 2024 DateFormat.yMMMMEEEEd(); // Monday, January 15, 2024 DateFormat.yMMMd(); // Jan 15, 2024 // 时间格式 DateFormat.Hm(); // 14:30 DateFormat.Hms(); // 14:30:45 DateFormat.jm(); // 2:30 PM DateFormat.jms(); // 2:30:45 PM // 本地化 DateFormat.yMMMMd('zh_CN'); // 2024年1月15日 DateFormat.yMMMMd('ja_JP'); // 2024年1月15日 DateFormat.yMMMMd('de_DE'); // 15. Januar 2024DateTime运算
DateTime now = DateTime.now(); // 增加时间-add DateTime tomorrow = now.add(Duration(days: 1)); DateTime nextHour = now.add(Duration(hours: 1)); DateTime nextWeek = now.add(Duration(days: 7)); // 减少时间-subtract DateTime yesterday = now.subtract(Duration(days: 1)); DateTime lastHour = now.subtract(Duration(hours: 1)); // 计算差异-difference DateTime date1 = DateTime(2024, 1, 1); DateTime date2 = DateTime(2024, 1, 15); Duration difference = date2.difference(date1); print(difference.inDays); // 14天时间比较
DateTime date1 = DateTime(2024, 1, 10); DateTime date2 = DateTime(2024, 1, 15); // 比较方法 print(date1.isBefore(date2)); // true print(date1.isAfter(date2)); // false print(date1.isAtSameMomentAs(date2)); // false // 检查两个 DateTime 是否表示同一时刻 print(date1.compareTo(date2)); // -1 (小于) //compareTo用于比较两个日期时间的先后顺序,返回一个整数表示比较结果,(正数或负数或0) // 排序-sort List<DateTime> dates = [date2, date1]; dates.sort(); // 自动按时间排序 // 判断是否在同一天 bool isSameDay(DateTime a, DateTime b) { return a.year == b.year && a.month == b.month && a.day == b.day; }