【问题标题】:How get the name of the days of the week in Dart如何在 Dart 中获取星期几的名称
【发布时间】:2019-01-25 19:44:35
【问题描述】:

有人知道如何从 DateTime 中提取星期几的名称吗?

ej:

DateTime date = DateTime.now();
String dateFormat = DateFormat('dd-MM-yyyy hh:mm').format(date);

结果 -> 星期五

【问题讨论】:

    标签: flutter datetime dart


    【解决方案1】:

    使用“EEEE”作为日期模式

     DateFormat('EEEE').format(date); /// e.g Thursday
    

    别忘了导入

    import 'package:intl/intl.dart';
    

    查看更多信息:https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html

    【讨论】:

    • 观众可能会失望,请说明需要添加国际包
    【解决方案2】:

    在您的 pubspec.yaml 文件中的依赖项部分。 像这样添加 intl 依赖:

    dependencies:
      intl: ^0.16.0 // <-- dependency added here, remember to remove this comment
      // other dependencies . remove this comment too
    

    这里有更多关于国际依赖的信息: intl

    此软件包提供国际化和本地化功能,包括消息翻译、复数和性别、日期/数字格式和解析以及双向文本。

    然后在您的 dart 文件导入的顶部:

    import 'package:intl/intl.dart';
    

    现在您可以根据需要使用 DateFormat,这是一个示例:

    var date = DateTime.now();
    print(date.toString()); // prints something like 2019-12-10 10:02:22.287949
    print(DateFormat('EEEE').format(date)); // prints Tuesday
    print(DateFormat('EEEE, d MMM, yyyy').format(date)); // prints Tuesday, 10 Dec, 2019
    print(DateFormat('h:mm a').format(date)); // prints 10:02 AM
    

    您可以在 DateFormat 中使用多种格式,更多详细信息可在此处找到: https://api.flutter.dev/flutter/intl/DateFormat-class.html

    希望这会有所帮助。 谢谢

    【讨论】:

      【解决方案3】:

      您可以使用 .weekday 方法来找出日期。 例如:

       DateTime date = DateTime.now();
       print("weekday is ${date.weekday}");
      

      这将返回工作日编号,例如 tuesday 是 2 ,如下所示

      class DateTime implements Comparable<DateTime> {
            // Weekday constants that are returned by [weekday] method:
            static const int monday = 1;
            static const int tuesday = 2;
            static const int wednesday = 3;
            static const int thursday = 4;
            static const int friday = 5;
            static const int saturday = 6;
            static const int sunday = 7;
            static const int daysPerWeek = 7;
      

      【讨论】:

      • 您不理解 OP 的问题。他要的是名字,而不是号码。
      • 谢谢。我正要问一个关于这个的问题。以int 的形式获取星期几。
      【解决方案4】:

      您可以使用以下方法来达到预期的效果。可以根据您的喜好更改退货声明。 (这是如果你想跳过使用 intl 包)

      String dateFormatter(DateTime date) {
        dynamic dayData =
            '{ "1" : "Mon", "2" : "Tue", "3" : "Wed", "4" : "Thur", "5" : "Fri", "6" : "Sat", "7" : "Sun" }';
      
        dynamic monthData =
            '{ "1" : "Jan", "2" : "Feb", "3" : "Mar", "4" : "Apr", "5" : "May", "6" : "June", "7" : "Jul", "8" : "Aug", "9" : "Sep", "10" : "Oct", "11" : "Nov", "12" : "Dec" }';
      
        return json.decode(dayData)['${date.weekday}'] +
            ", " +
            date.day.toString() +
            " " +
            json.decode(monthData)['${date.month}'] +
            " " +
            date.year.toString();
      }
      

      上面的方法可以调用为

      dateFormatter(DateTime.now())
      

      产生结果 - 2020 年 4 月 11 日,星期六

      【讨论】:

        【解决方案5】:

        扩展是很好的解决方案。工作日也可以。

        extension DateTimeExtension on DateTime {
          String getMonthString() {
            switch (month) {
              case 1:
                return 'Jan.';
              case 2:
                return 'Feb.';
              case 3:
                return 'Mar.';
              case 4:
                return 'Apr.';
              case 5:
                return 'May';
              case 6:
                return 'Jun.';
              case 7:
                return 'Jul.';
              case 8:
                return 'Aug.';
              case 9:
                return 'Sept.';
              case 10:
                return 'Oct.';
              case 11:
                return 'Nov.';
              case 12:
                return 'Dec.';
              default:
                return 'Err';
            }
          }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-01-04
          • 1970-01-01
          • 2011-11-11
          • 1970-01-01
          • 2021-07-10
          • 2018-08-09
          • 1970-01-01
          • 2019-05-18
          相关资源
          最近更新 更多