Sample code for flutter to get the timestamp 5 minutes after the current time:

Sample code to get the timestamp 5 minutes after the current time:

void main() {
    
    
  var now = DateTime.now();
  var fiveMinutesLater = now.add(Duration(minutes: 5));
  var fiveMinutesLaterTimestamp = fiveMinutesLater.millisecondsSinceEpoch ~/ 1000;
  print(fiveMinutesLaterTimestamp); // 输出当前时间后5分钟的时间戳
}
  //获取结束禁言的时间戳
  getStopTextTime(int type, int value) {
    
    
    //type{2:几天,3:几周,4:几月,5:几分钟,6:几小时}
    int time = 0;
    var now = DateTime.now();
    var fiveMinutesLater;
    if (type == 2) {
    
    
      fiveMinutesLater = now.add(Duration(days: value));
    } else if (type == 3) {
    
    
      fiveMinutesLater = now.add(Duration(days: value * 7));
    } else if (type == 4) {
    
    
      fiveMinutesLater = now.add(Duration(days: value * 30));
    } else if (type == 5) {
    
    
      fiveMinutesLater = now.add(Duration(minutes: value));
    } else if (type == 6) {
    
    
      fiveMinutesLater = now.add(Duration(hours: value));
    }
    var fiveMinutesLaterTimestamp =
        fiveMinutesLater.millisecondsSinceEpoch ~/ 1000;
    print(
        '现在的时间戳是${
    
    DateTime.now().millisecondsSinceEpoch}   禁言结束的时间戳是多少$fiveMinutesLaterTimestamp');

    return time;
  }

In the above example, we used DateTime.now()to get the current time, then used add()the method to add 5 minutes, and finally used to millisecondsSinceEpochget the timestamp, the obtained timestamp needs to be divided by 1000 to round up, because millisecondsSinceEpochthe returned timestamp is millisecond level .

Guess you like

Origin blog.csdn.net/weixin_44911775/article/details/129983873