我有一个DateTime的实例,我想将其格式化为字符串。我怎么做呢?我想把日期转换成一个字符串,类似于“2013-04-20”。


当前回答

要获得一个用户友好的字符串,包括日期和时间,你可以使用这个:

import 'package:intl/intl.dart';

main() {
  final DateTime someDateTime = DateTime(2017, 9, 7, 17, 30);
  final DateFormat formatter = DateFormat.yMMMd().add_jms();
  final String formatted = formatter.format(someDateTime);
  print(formatted); // Sep 7, 2017 5:30:00 PM (in the US)
}

其他回答

import 'package:intl/intl.dart';

main() {
  var formattedDate = new DateTime.Format('yyyy-MM-dd').DateTime.now();
  print(formattedDate); // something like 2020-04-16
}

有关更多详细信息,请参阅DateFormat文档

您还可以像前面所述的那样指定日期格式:https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html

import 'package:intl/intl.dart';
String formatDate(DateTime date) => new DateFormat("MMMM d").format(date);

出品时间:3月4日

这就给你提供了社交网络中的日期:[“今天”,“昨天”,“星期的哪一天”,等等。]

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 dateConverter(String date) {
    // Input date Format
    final format = DateFormat("dd-MM-yyyy");
    DateTime gettingDate = format.parse(date);
    final DateFormat formatter = DateFormat('yyyy-MM-dd');
    // Output Date Format
    final String formatted = formatter.format(gettingDate);
    return date;
  }

无依赖项方法[用于将日期显示为格式化字符串]

如果你想要以日/月/年的形式显示你的DateTime值或任何你喜欢的格式,字符串插值可以很方便:

"${_date.day} / ${_date.month} / ${_date.year}"

示例输出:

It's 23 out of 4 out of 1920

我不想使用任何额外的库,所以我走了这条路。