如何在 Flutter 中使用 syncfusion_flutter_datepicker 仅获取 DateTime 格式的 "date"

How to get only "date" in DateTime format using syncfusion_flutter_datepicker in Flutter

我想要什么: 我只想获取 DateTime 格式的 startDate 和 endDate,这样我就可以找到两个日期之间的天数差异。相反,我以字符串格式获取 startDate 和 endDate。

我的代码:

  void _onSelectionChanged(DateRangePickerSelectionChangedArgs args) {
    print("Printing start date before setstate");
    print(startDate);
    setState(() {
      if (args.value is PickerDateRange) {
        _range = DateFormat('dd/MM/yyyy')
                .format(args.value.startDate)
                .toString() +
            
            ' - ' +
            DateFormat('dd/MM/yyyy')
                .format(args.value.endDate ?? args.value.startDate)
                .toString();
 } else if (args.value is DateTime) {
        _selectedDate = args.value.toString();
      } else if (args.value is List<DateTime>) {
        _dateCount = args.value.length.toString();
      } else {
        _rangeCount = args.value.length.toString();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.teal[50],
      body: Container(
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              Container(
                height: MediaQuery.of(context).size.height * 0.40,
                child: Card(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(25.0)),
                  child: SfDateRangePicker(
                    view: DateRangePickerView.month,
                    monthViewSettings: DateRangePickerMonthViewSettings(
                        showTrailingAndLeadingDates: true),
                    selectionColor: Colors.teal,
                    startRangeSelectionColor: Colors.teal[400],
                    endRangeSelectionColor: Colors.teal[400],
                    rangeSelectionColor: Colors.teal[200],
                    rangeTextStyle:
                        const TextStyle(color: Colors.white, fontSize: 15),
                    onSelectionChanged: _onSelectionChanged,
                    selectionMode: DateRangePickerSelectionMode.range,
                    initialSelectedRange: PickerDateRange(
                      DateTime.now(),
                      DateTime.now().add(
                        const Duration(days: 5),
                      ),
                    ),
                  ),
                ),
              ),

我尝试过的: 我试着像这样打印开始日期:

        print(args.value.startDate);
        final DateFormat formatter = DateFormat('yyyy-MM-dd');
        final String formattedStartDate =
            formatter.format(args.value.startDate);
        print(formattedStartDate); // printing only date

我成功获取了日期,但它是String类型而不是DateTime类型。

如果您想从 String 中获取 DateTime 对象,请执行以下操作:

DateFormat('yyyy-MM-dd').parse('some string date')

尝试在方法差异中使用要求格式的日期。

var berlinWallFell = DateTime(1989, DateTime.november, 9);
var dDay = DateTime(1944, DateTime.june, 6);
Duration difference = berlinWallFell.difference(dDay);
assert(difference.inDays == 16592);

Link: https://api.flutter.dev/flutter/dart-core/DateTime/difference.html