0

My plan is to select 2 dates

  1. start Date
  2. end Date

Where I should get the start date as "20/08/2021 12:00 AM" and end date as "20/08/2021 11:59 PM".

But I am only getting the start Date as "20/08/2021 12:00 AM" not the end Date as "20/08/2021 11:59 PM" , here I am focusing the end Time i.e.,11:59 PM, which is the end time of a day in 12 hour Format, and I am trying to get it by default while picking the end Date.

And for this I already checked the official documentation , I didn't found any resolution.

For reference https://api.flutter.dev/flutter/intl/DateFormat-class.html

Here is the code

static DateTime nope = DateTime.now();


var selectedStartDate = DateFormat('dd/MM/yyyy hh:mm a');
  // var date1 = DateFormat('dd/MM/yyyy').format(nope);
  Future<Null> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
        context: context,
        initialDate: nope,
        firstDate: DateTime(2000, 8),
        lastDate: DateTime(2101));
    if (picked != null && picked != nope)
      setState(() {
        nope = picked;
      });
    startDate.text = selectedStartDate.format(nope);
  }

  static DateTime yep = DateTime.now();
  var selectEndDate = DateFormat('dd/MM/yyyy');
  var date2 = DateFormat('dd/MM/yyyy').format(yep);
  Future<Null> _selecteddate(BuildContext context) async {
    final DateTime pick = await showDatePicker(
        context: context,
        initialDate: yep,
        firstDate: DateTime(2000, 8),
        lastDate: DateTime(2101));
    if (pick != null && pick != yep)
      setState(() {
        yep = pick;
      });
    endDate.text = selectEndDate.format(yep);
  }

Here is the output, how I am getting from the above code

enter image description here

Please do help me in searching the solution and Thanks in advance .

Spsnamta
  • 479
  • 6
  • 11

2 Answers2

0

Maybe replace

var selectEndDate = DateFormat('dd/MM/yyyy');

with

var selectEndDate = DateFormat('dd/MM/yyyy hh:mm a');
Quasi
  • 576
  • 4
  • 13
  • Thanks for the answer but I already tried this one and I am getting "20/08/2021 12:00 AM" but it should be "20/08/2021 11:59 PM", please check the time. – Spsnamta Aug 20 '21 at 11:39
0

I'm building a POS project now, So I had the same Start Date and End Date Fields in my Tax and other Reports sections. When I faced the same issue I fixed it by adding 23 hours and 59 minutes to the selected date.

I use the below Class to pick a date throughout my application

class DateTimeUtils {
 //========== Singleton Instance ==========
 DateTimeUtils._internal();
 static DateTimeUtils instance = DateTimeUtils._internal();
 factory DateTimeUtils() {
  return instance;
}

//========== Date Picker ==========
Future<DateTime?> datePicker(BuildContext context, {final DateTime? initDate, final bool endDate = false}) async {
 final DateTime? selectedDate = await showDatePicker(
  context: context,
  initialDate: initDate ?? DateTime.now(),
  firstDate: DateTime(1998, 04, 14),
  lastDate: DateTime.now(),
);

if (selectedDate != null && endDate) {
  final DateTime updated = selectedDate.add(const Duration(hours: 23, minutes: 59));
  return updated;
} else {
  return selectedDate;
  }
 }
}

Can be called from anywhere in the project like below

final _selectedDate = await DateTimeUtils.instance.datePicker(context, initDate: toDate, endDate: true);