我有一个DateTime的实例,我想将其格式化为字符串。我怎么做呢?我想把日期转换成一个字符串,类似于“2013-04-20”。
当前回答
这就给你提供了社交网络中的日期:[“今天”,“昨天”,“星期的哪一天”,等等。]
void main() {
DateTime now = new DateTime(2018,6,26);
print(date(now));
}
String date(DateTime tm) {
DateTime today = new DateTime.now();
Duration oneDay = new Duration(days: 1);
Duration twoDay = new Duration(days: 2);
Duration oneWeek = new Duration(days: 7);
String month;
switch (tm.month) {
case 1:
month = "january";
break;
case 2:
month = "february";
break;
case 3:
month = "march";
break;
case 4:
month = "april";
break;
case 5:
month = "may";
break;
case 6:
month = "june";
break;
case 7:
month = "july";
break;
case 8:
month = "august";
break;
case 9:
month = "september";
break;
case 10:
month = "october";
break;
case 11:
month = "november";
break;
case 12:
month = "december";
break;
}
Duration difference = today.difference(tm);
if (difference.compareTo(oneDay) < 1) {
return "today";
} else if (difference.compareTo(twoDay) < 1) {
return "yesterday";
} else if (difference.compareTo(oneWeek) < 1) {
switch (tm.weekday) {
case 1:
return "monday";
case 2:
return "tuesday";
case 3:
return "wednesday";
case 4:
return "thursday";
case 5:
return "friday";
case 6:
return "saturday";
case 7:
return "sunday";
}
} else if (tm.year == today.year) {
return '${tm.day} $month';
} else {
return '${tm.day} $month ${tm.year}';
}
}
其他回答
String formatDate(String date) {
return date.substring(0, 10).split('-').reversed.join().replaceAll('-', '/');
}
From format "yyyy-mm-dd H:i:s"
由于flutter常用的Datetime库不知道德国的区域设置——de、de、de_DE、de- de或任何其他组合都不知道,例如,如果您希望用人类可读的字符串编写月份名称或日期名称,我建议您使用以下代码。
安装pub flutter pub add date_format
String formattedDate = formatDate(DateTime(year, month, day),[dd,'. ', MM, ' ', yyyy], locale: GermanDateLocale());
/// Get date as a string for display.
String getFormattedDate(String date) {
/// Convert into local date format.
var localDate = DateTime.parse(date).toLocal();
/// inputFormat - format getting from api or other func.
/// e.g If 2021-05-27 9:34:12.781341 then format must be yyyy-MM-dd HH:mm
/// If 27/05/2021 9:34:12.781341 then format must be dd/MM/yyyy HH:mm
var inputFormat = DateFormat('yyyy-MM-dd HH:mm');
var inputDate = inputFormat.parse(localDate.toString());
/// outputFormat - convert into format you want to show.
var outputFormat = DateFormat('dd/MM/yyyy HH:mm');
var outputDate = outputFormat.format(inputDate);
return outputDate.toString();
}
import 'package:intl/intl.dart';
main() {
var formattedDate = new DateTime.Format('yyyy-MM-dd').DateTime.now();
print(formattedDate); // something like 2020-04-16
}
有关更多详细信息,请参阅DateFormat文档
设置你的项目intl包
dateTimeFormet (date){
//MM-dd-yyyy
//yyyy-MM-dd
return DateFormat('dd-MM-yyyy').format(date);// you can set your formet
}
void main (){
var date = 01-11-2022 00 : 00
var _datetime = dateTimeFormet(date);
print(_dateTime);
}
推荐文章
- 如何检查Flutter应用程序是否正在调试中运行?
- 在Flutter中向有状态小部件传递数据
- 未处理异常:ServicesBinding.defaultBinaryMessenger在绑定初始化之前被访问
- 颤振-换行文本
- 如何在Dart中四舍五入到小数点后的给定精度程度?
- 添加一个启动屏幕颤振应用程序
- 在flutter中等同于wrap_content和match_parent ?
- 多行文本字段在扑动
- 在Dart中命名参数和位置参数之间有什么区别?
- 如何用Dart将字符串解析为数字?
- 如何在颤振的一些延迟后运行代码?
- 在构建过程中调用setState()或markNeedsBuild
- 我如何添加阴影的小部件颤振?
- 如何处理不需要的小部件构建?
- 如何在扑动中设置背景图像?