【问题标题】:How to format DateTime in Flutter? Remove milliseconds in DateTime?如何在 Flutter 中格式化 DateTime?删除 DateTime 中的毫秒数?
【发布时间】:2021-05-26 16:38:18
【问题描述】:

如您所见,我有一个自定义 DatePicker 小部件,它采用 DateTime 类型的 currentTime

 DatePickerWidget(
                    currentTime: DateTime.now(),
                    text: "$date1",
                    onChanged: (date) {
                      setState(() {
                        getProductDate(date.toString());
                        this.date1 = date.toString();
                      });
                    },
                    onConfirm: (date) {
                      setState(() {
                        getProductDate(date.toString());

                        this.date1 = date.toString();
                      });
                    },
                  ),

但它也给了我几毫秒。

result

结果

YYYY-MM-JJ HH-MM:00.000

如何删除 DateTime 类型中的 :00.000 部分?

我只想要这种格式:'yyyy-MM-dd'

currentTime 只获得 DateTime 类型。

有什么想法吗?

我的 DatePickerWidget 代码:

class DatePickerWidget extends StatelessWidget {
  final Function(DateTime data) onChanged;
  final Function(DateTime data) onConfirm;
  final String text;
  final DateTime currentTime;

 
 

  const DatePickerWidget(
      {Key key, this.onChanged, this.onConfirm, this.text, this.currentTime})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ButtonWidget(
      onPressed: () {
        DatePicker.showDatePicker(context,
            theme: DatePickerTheme(
              containerHeight: context.dynamicHeight(0.3),
            ),
            showTitleActions: true,
            minTime: DateTime(2021, 1, 1),
            maxTime: DateTime(2028, 12, 29),
            onChanged: onChanged,
            onConfirm: onConfirm,
            currentTime: currentTime,
            locale: LocaleType.tr);
      },
      text: text,
      buttonColor: Colors.white,
      borderColor: Colors.black,
      textColor: Colors.black,
    );
  }
}

【问题讨论】:

  • 不,我也已经检查过了。我的问题不同:/
  • 不同如何?这是一个精确的副本。为什么你的问题不一样?
  • 先生,我的当前时间只有“日期时间”类型。我检查了您问题中的所有解决方案,但对我不起作用:(
  • 哪个代码显示日期?我的意思是从你的图像。

标签: flutter datetime dart


【解决方案1】:

您可以使用来自intl 包的DateFormat

import 'package:intl/intl.dart';

DateTime now = DateTime.now();
String formattedDate = DateFormat('yyyy-MM-dd – kk:mm').format(now);

你也可以在不添加依赖项的情况下这样做

DateTime.now()
            .toString()
            .substring(0,10)
     );   

0

【讨论】:

  • 我的 currentTime 仅获得“DateTime”类型。所以你的代码对我不起作用:/
【解决方案2】:

您将需要包intl

final now = DateTime.now();
String dateFormatted = DateFormat('yyyy-MM-dd').format(now);

【讨论】:

  • 我的 currentTime 仅获得“DateTime”类型。所以你的代码对我不起作用:/
  • 它不会改变你只需要做的任何事情String dateFormatted = DateFormat('yyyy-MM-dd').format(currentTime);
【解决方案3】:

试试Jiffy,让处理日期和时间变得更容易

要格式化您的 DateTime,只需传入您的结果 date,见下文

this.date1 = Jiffy(date).format('yyyy-MM-dd'); // 2021-03-24

// or you can also use default formats

this.date1 = Jiffy(date).yMMMMd; // March 24, 2021

【讨论】:

    【解决方案4】:

    这里用“MillisecondsSinceEpoch”代替你的值

    var startDate = DateTime.fromMillisecondsSinceEpoch(int.parse("MillisecondsSinceEpoch"));
    

    【讨论】:

    • 值是什么样的?
    • 您转换后的 MillisecondsSinceEpoch
    猜你喜欢
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 2011-05-23
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    相关资源
    最近更新 更多