【问题标题】:How to get only "date" in DateTime format using syncfusion_flutter_datepicker in Flutter如何在 Flutter 中使用 syncfusion_flutter_datepicker 仅获取 DateTime 格式的“日期”
【发布时间】:2026-01-18 02:20:04
【问题描述】:

我想要什么: 我只想获取 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),
                      ),
                    ),
                  ),
                ),
              ),

我尝试过的: 我尝试像这样打印 startDate:

        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 类型。

【问题讨论】:

    标签: flutter dart dart-null-safety flutter-date-range-picker


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 感谢您的回复。真的很有帮助。