Home > Net >  How can i increase only 1 microsecond to Dateime.now() in dart
How can i increase only 1 microsecond to Dateime.now() in dart

Time:04-24

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

CodePudding user response:

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);

CodePudding user response:

  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/70848330/

  • Related