如何在Flutter中格式化DateTime?如何移除DateTime中的毫秒?

11

正如你所见,我有一个自定义的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();
                      });
                    },
                  ),

但是它也给了我毫秒。

结果

结果

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,如何获取当前时间? - nvoigt
先生,我的currentTime只能获得“DateTime”类型。我检查了您所有问题的解决方案,但对我没有用。 :( - Soner Karaevli
你的图片中哪段代码显示了日期? - John Joe
"$date1" 正在显示日期。如果用户选择了日期,我将把所选日期显示在按钮文本中。 - Soner Karaevli
为什么这些答案都没有帮到你呢?任何一个都能产生你想要的输出。也许是我没有正确理解你的问题? - nvoigt
显示剩余2条评论
4个回答

10

你可以使用来自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”类型。所以你的代码在我这里不起作用 :/ - Soner Karaevli

2
您需要使用包intl
final now = DateTime.now();
String dateFormatted = DateFormat('yyyy-MM-dd').format(now);

我的currentTime只获取“DateTime”类型。所以你的代码对我不起作用 :/ - Soner Karaevli
它不会改变任何东西,你只需要执行 String dateFormatted = DateFormat('yyyy-MM-dd').format(currentTime); - Guillaume Roux

2

试用Jiffy,使得处理日期和时间更加容易

要为你的DateTime格式化,只需传入你的结果 date,如下所示

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

// or you can also use default formats

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

0
在此处替换 "MillisecondsSinceEpoch" 为您的值
var startDate = DateTime.fromMillisecondsSinceEpoch(int.parse("MillisecondsSinceEpoch"));

价值是什么样的? - Soner Karaevli
你的毫秒数自Epoch起 - Meetkumar Shah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接