flutter 获取当前时间后5分钟的时间戳示例代码:

获取当前时间后5分钟的时间戳示例代码:

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

在上面的示例中,我们使用了DateTime.now()来获取当前时间,然后使用add()方法来增加5分钟的时间,最后使用millisecondsSinceEpoch获取时间戳,得到的时间戳需要除以1000取整,因为millisecondsSinceEpoch返回的是毫秒级别的时间戳。

猜你喜欢

转载自blog.csdn.net/weixin_44911775/article/details/129983873