我想在顫振中找到兩個不同日期時間之間的天數差異。
換句話說,我想減去兩個日期并得到它們之間的差異
我找到了一種在 c# here 中做到這一點的方法。但不幸的是,這不適用于顫振。
任何幫助表示贊賞
uj5u.com熱心網友回復:
您可以使用類的difference方法DateTime來獲取飛鏢中兩個日期之間的持續時間。
DateTime date1 = new DateTime.now();
DateTime date2 = DateTime(2021, 1, 1);
print(date1.difference(date2).inDays); // Prints 356 days
uj5u.com熱心網友回復:
// 獲取當前日期和時間
DateTime startDate = DateTime.now();
String currentTime = formatISOTime(startDate);
//將日期轉換為字串
String formatISOTime(DateTime date) {
var duration = date.timeZoneOffset;
if (duration.isNegative)
return (date.toIso8601String()
"-${duration.inHours.toString().padLeft(2, '0')}:${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}");
else
return (date.toIso8601String()
" ${duration.inHours.toString().padLeft(2, '0')}:${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}");
}
//計算時差
String? timediffference = calculateTimeDifferenceBetween(
givenDate ?? '',
currentTime) ??
'',
static String? calculateTimeDifferenceBetween(
String? givenDate, String? currentTime) {
if (givenDate == null ||
givenDate.isEmpty ||
currentTime == null ||
currentTime.isEmpty) {
return "";
} else {
String gDate = givenDate.replaceAll('T', ' ');
String curTime = currentTime.replaceAll('T', ' ');
DateTime startParseDate =
new DateFormat("yyyy-MM-dd HH:mm:ss").parse(curTime);
DateTime endParseDate =
new DateFormat("yyyy-MM-dd HH:mm:ss").parse(gDate);
Duration diff = startParseDate.difference(endParseDate);
if (diff.inDays >= 1) {
return '${diff.inDays} day ago';
} else if (diff.inDays >= 30) {
return '${diff.inDays % 30} months ago';
} else if (diff.inDays >= 365) {
return '${diff.inDays % 365} years ago';
} else if (diff.inHours >= 1) {
return '${diff.inHours} hours ago';
} else if (diff.inHours < 1) {
if (diff.inMinutes >= 1) {
return '${diff.inMinutes} minutes ago';
} else if (diff.inMinutes < 1) {
return 'just now ';
} else {
return 'just now ';
}
} else {
return 'just now ';
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/391682.html
上一篇:通過點擊每個串列的標題而不是顫動中的右箭頭來打開和關閉擴展面板串列
下一篇:帶有未初始化引數錯誤的單例模式
