flutter 如何得到一周几天的名称在达特

qfe3c7zg  于 2023-05-01  发布在  Flutter
关注(0)|答案(7)|浏览(133)

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

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

结果-〉星期五

w8rqjzmb

w8rqjzmb1#

使用'EEEE'作为日期模式

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

别忘了导入

import 'package:intl/intl.dart';

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

e1xvtsh3

e1xvtsh32#

在你的pubspec。yaml文件,依赖项部分。添加intldependency如下:

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

更多关于intl依赖的信息: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
希望这对你有帮助。谢谢你

bmp9r5qi

bmp9r5qi3#

你可以用。weekday方法来找出这一天。例如:

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

这将返回工作日的数字,例如星期二是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;
jm81lzqq

jm81lzqq4#

您可以使用下面的方法来实现所需的结果。return语句可以根据您的喜好进行更改。(这是如果你想跳过使用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())

The produces the result -周六,11 Apr 2020

px9o7tmv

px9o7tmv5#

扩展是很好的解决方案。你也可以在工作日做。在使用之前,请调用一次DateTimeExtension以查看getMonthString()

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';
    }
  }
}
lpwwtiir

lpwwtiir6#

我认为最好的解决方案是使用intl包,如其他答案所示,但如果你想编写自己的解决方案,你可以这样做:

const Map<int, String> weekdayName = {1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday", 7: "Sunday"};
print(weekdayName[DateTime.now().weekday]);

或者你可以扩展DateTime类,做一些类似的事情:

extension DateTimeExtension on DateTime {
  String? weekdayName() {
    const Map<int, String> weekdayName = {1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday", 7: "Sunday"};
    return weekdayName[weekday];
  }
}
print(DateTime.now().weekdayName());

可以在DartPad1DartPad2中测试这些解决方案

dgsult0t

dgsult0t7#

如果你想获得本地化的名称,防弹解决方案专用工作日,你需要有点棘手。
让我们设计API:

String getWeekdayName(int weekday)

我们当然知道我们可以从DateTime得到工作日:

DateTime.weekday // returns ISO value

它返回一个ISO值(Monday = 1,Sunday = 7
我们还知道,要获得“Monday”,“Tuesday”(或任何本地化值),我们可以这样做:

DateFormat('EEEE').format(date);  // returns String

那么如何获得int weekday的名称呢?

我们应该用weekday以某种方式创建DateTime,然后相应地格式化它。

我们还必须为weekday传递一个符合ISO的值,该值在<1,7>范围内。
解决方案:

final DateTime now = DateTime.now().toLocal();
final int diff = now.weekday - weekday; // weekday is our 1-7 ISO value
var udpatedDt;
if (diff > 0) {
    udpatedDt = now.subtract(Duration(days: diff));
} else if (diff == 0) {
    udpatedDt = now;
} else {
    udpatedDt = now.add(Duration(days: diff * -1));
}
final String weekdayName = DateFormat('EEEE').format(udpatedDt);
return weekdayName;

相关问题