You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter:如何获取次日00:00:00的DateTime对象?

实现次日00:00:00的几种方法

在Dart中,你可以通过以下几种方式获取次日的零点时间:

方法一:直接构造指定日期

通过当前日期的年、月,直接传入日+1并设置时分秒为0,这种方式会自动处理月末、年末的日期进位:

DateTime? created_at = DateTime.now(); // 当前时间假设为Jul 25 10:35:90
DateTime start_date = DateTime(
  created_at.year,
  created_at.month,
  created_at.day + 1,
  0, 0, 0, 0
);
// 最终start_date的值为Jul 26 00:00:00

方法二:添加天数后重置时间

先给当前日期加1天,再通过copyWith方法将时分秒及以下单位重置为0:

DateTime? created_at = DateTime.now();
DateTime start_date = created_at.add(const Duration(days: 1)).copyWith(
  hour: 0,
  minute: 0,
  second: 0,
  millisecond: 0,
  microsecond: 0
);

注意事项

  • 两种方法都能自动处理跨月、跨年场景(比如1月31日执行后会得到2月1日零点)
  • 如果需要UTC时区的次日零点,可将DateTime.now()替换为DateTime.utc()

内容的提问来源于stack exchange,提问作者Arijeet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 21:06:23