func readTimestamp(timestamp: Int) {
let now = Date()
let dateFormatter = DateFormatter()
let date = Date(timeIntervalSince1970: Double(timestamp))
let components = Set<Calendar.Component>([.second, .minute, .hour, .day, .weekOfMonth])
let diff = Calendar.current.dateComponents(components, from: date, to: now)
var timeText = ""
dateFormatter.locale = .current
dateFormatter.dateFormat = "HH:mm a"
if diff.second! <= 0 || diff.second! > 0 && diff.minute! == 0 || diff.minute! > 0 && diff.hour! == 0 || diff.hour! > 0 && diff.day! == 0 {
timeText = dateFormatter.string(from: date)
}
if diff.day! > 0 && diff.weekOfMonth! == 0 {
timeText = (diff.day == 1) ? "\(diff.day!) DAY AGO" : "\(diff.day!) DAYS AGO"
}
if diff.weekOfMonth! > 0 {
timeText = (diff.weekOfMonth == 1) ? "\(diff.weekOfMonth!) WEEK AGO" : "\(diff.weekOfMonth!) WEEKS AGO"
}
return timeText
}
String readTimestamp(int timestamp) {
var now = new DateTime.now();
var format = new DateFormat('HH:mm a');
var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp);
var diff = date.difference(now);
var time = '';
if (diff.inSeconds <= 0 || diff.inSeconds > 0 && diff.inMinutes == 0 || diff.inMinutes > 0 && diff.inHours == 0 || diff.inHours > 0 && diff.inDays == 0) {
time = format.format(date); // Doesn't get called when it should be
} else {
time = diff.inDays.toString() + 'DAYS AGO'; // Gets call and it's wrong date
}
return time;
}
String readTimestamp(int timestamp) {
var now = new DateTime.now();
var format = new DateFormat('HH:mm a');
var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000);
var diff = date.difference(now);
var time = '';
if (diff.inSeconds <= 0 || diff.inSeconds > 0 && diff.inMinutes == 0 || diff.inMinutes > 0 && diff.inHours == 0 || diff.inHours > 0 && diff.inDays == 0) {
time = format.format(date);
} else {
if (diff.inDays == 1) {
time = diff.inDays.toString() + 'DAY AGO';
} else {
time = diff.inDays.toString() + 'DAYS AGO';
}
}
return time;
}
您的时间戳格式实际上是以秒为单位(Unix时间戳),而不是以微秒为单位。如果是,答案如下:
变化:
var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp);
到
var date = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
在flutter示例页面中,有一个名为“将数据发送到新屏幕”的项目。我对第65行的构造函数有一个重新保护的问题。
我试图改变从API返回的日期字符串的格式。下面的日期格式输入字符串在java中工作正常,但在Dart中则不行。
我已经在我的服务器上创建了一个RSA密钥对,只有服务器才会有私钥。客户端(Flatter应用程序)将访问公钥。因此,当服务器收到加密消息时,它将知道消息来自正确的客户端,并将使用其私钥解密通过公钥加密的消息。 当服务器(php7.3)向客户端发送回复时,我想在服务器端通过私钥加密消息,以便客户端可以用公钥解密消息。 非对称加密有其自身的局限性,如最大字符长度。但如果能送来就好了 Flutter包加
我如何重新格式化、转换或从Dart/Flutter中的字符串中提取日期和时间?字符串来自我的MySQL数据库中的默认时间戳; 但是我不确定用什么作为regex。
我一直在使用VSCode进行颤振开发,但没有问题。今天,飞镖分析器突然抱怨说,大多数类都是未定义的,或者目标不存在。 有人见过这个吗? 我已经重新启动了VSCode,重新启动了计算机,卸载了flutter/dart插件,运行flutter dr没有任何问题……我没有想法了。 VSCode问题
[![Plugins installed][1][1]我已经在android studio中安装了这些插件,并将C:\flatter\bin添加到系统环境变量中,但当我在PowerShell中运行flatter doctor时,它显示这些插件没有安装。我用的是Windows操作系统。