我如何仅将1微秒增加到dateTime.now()

发布于 2025-01-23 11:02:28 字数 271 浏览 3 评论 0原文

我有以下

Dateime.now() + here i need to increase only 1 microsecond

想要的输出是dateime.now() + 1 microsecond

我尝试了以下

print(DateTime.now()+const Duration(microsecond : 1);)

内容

i have the following

Dateime.now() + here i need to increase only 1 microsecond

so wanted output is Dateime.now() + that incensement which is 1 microsecond

i tried the following but it does not work

print(DateTime.now()+const Duration(microsecond : 1);)

How can i implement this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七月上 2025-01-30 11:02:28

这很好地完成了。

final now = DateTime.now();
final later = now.add(const Duration(millisecond: 1));

检查文档在这里

或在一行中进行:

DateTime now = DateTime.now().add(Duration(milliseconds: 1));
print(now);

This well get it done.

final now = DateTime.now();
final later = now.add(const Duration(millisecond: 1));

check docs here

Or do it in a single line:

DateTime now = DateTime.now().add(Duration(milliseconds: 1));
print(now);
巴黎盛开的樱花 2025-01-30 11:02:28
  1. dateTime不能定义操作员 +,但它确实具有添加方法接受持续时间。 (您的代码中还有几个语法错误;分号是放错了位置,命名参数为持续时间microseconds,而不是microsecond 。)

  2. ​在DART vm 中运行以下代码将显示微秒的更改:

      void main(){
      var now = dateTime.now();
      const微秒=持续时间(微秒:1);
      打印(现在); //印刷:2022-04-23 20:39:28.295803
      print(now.add(微秒)); //印刷:2022-04-23 20:39:28.295804
    }
     

    另请参阅: https://stackoverflow.com/a/a/607477710/

  1. DateTime does not define an operator +, but it does have an add method that accepts a Duration. (You also have a couple of syntax errors in your code; the semicolon is misplaced, and the named parameter to Duration is microseconds, not microsecond.)

  2. If you're testing with Dart for the Web (such as with DartPad), you will not get microsecond precision due to limitations with JavaScript. Running the following code in the Dart VM will show a change in microseconds:

    void main() {
      var now = DateTime.now();
      const microsecond = Duration(microseconds: 1);
      print(now);                  // Prints: 2022-04-23 20:39:28.295803
      print(now.add(microsecond)); // Prints: 2022-04-23 20:39:28.295804
    }
    

    Also see: https://stackoverflow.com/a/60747710/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文