如何在Dart获得最后一个午夜? [英] How to get last midnight in Dart?

查看:72
本文介绍了如何在Dart获得最后一个午夜?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这在大多数语言中都是很常见的任务,但是我不清楚如何在Flutter应用程序中完成此任务。如何在Dart中检索最后一个午夜的DateTime对象?还是可能,今天/明天/昨天的任何特定时间?

I guess this is pretty common task in most languages, however it was not clear to me how to get this done in my Flutter app. How to retrieve DateTime object of the last midnight in Dart? Or potentially, any particular time today / tomorrow / yesterday?

推荐答案

这应该做同样的事情

final now = DateTime.now();
final lastMidnight = new DateTime(now.year, now.month, now.day);

您可以使用这种方式获取当天的不同时间

You can use this way to get different times of this day

final tomorrowNoon = new DateTime(now.year, now.month, now.day + 1, 12);

final yesterdaySixThirty = new DateTime(now.year, now.month, now.day - 1, 6, 30);

根据需要,绝对值会更安全,因为加/减由于leap年和夏令时,持续时间可能会导致意外。

Depending on what you want can absolute values be more safe because adding/subtracting Duration can result in surprises because of leap years and daylight saving time.

这篇关于如何在Dart获得最后一个午夜?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆