Default DatePicker to last day of previous month

Hello anyone,
I have a date picker i’m trying to default to the last day of the previous month from today’s date. I have a function to get the same, problem is when I pick a date in a month let’s say March, if i try to alter the date again, I can’t seem to go forward beyond March.
Below is a snippet of the code and a screen grab

   Future _chooseDate(BuildContext context, String initialDateString) async {
var now = new DateTime.now().subtract(Duration(days: 30));
var formatter = new DateFormat('yyyy-MM-dd');
var initialDate = convertToDate(initialDateString) ?? now;
initialDate = (initialDate.year >= 2020 && initialDate.isBefore(now)
    ? initialDate
    : now);
var result = await showRoundedDatePicker(
    context: context,
    initialDate: initialDate,
    firstDate: new DateTime(2019,DateTime.december),
    lastDate: DateTime(initialDate.year, initialDate.month + 1, 0)); 
var firstDayOfMonth = new DateTime(result.year, result.month, 1);
var lastDayOfMonth = new DateTime(result.year, result.month + 1, 0);
if (result == null) return;
setState(() {

  taxPeriodStartDate.text = formatter.format(firstDayOfMonth);
  taxPeriodEndDate.text = formatter.format(lastDayOfMonth);
});

}

here aren’t you setting the DatePicker lastDate to one month after the initialDate?
This seems to explain why the picker does not show any subsequent dates. But I’m only guessing it by reading the code, without going to read the api docs about the picker…

@patrick_dm thanks. I’m setting it to the last date of the previous month. In the screen shot, that is the correct value I expect any time I select to choose date within July. But if I accidentally choose date in May and what to choose June instead, the datepicker is stuck in May and I can’t choose a future date.

I’ll go and read the api docs, it seems I’m mis-interpreting the meaning of the lastDate property and the value you are setting in in with

lastDate: DateTime(initialDate.year, initialDate.month + 1, 0));

yes so here is where i need to set it, so as to disable ant future dates.

I think I’m missing something, so sorry if this is misleading, but:

if you disable any future dates…

… you can’t choose a future date

:thinking:

Oh i see where your confusion is. Well the future dates i am trying to disable are those from the last day of the previous month. If i selected February, the next time I press the datepicker, it should still be able to go on until 30th June.

I really do not know why this was bugging me :angry:… But i’ve managed to achieve what i was looking for with this line of code
image
Thanks…

1 Like

When debugging there is nothing better than explaining your code and goals to someone else. If nobody is around then do explain it to your favourite rubber duck! :grin:

1 Like

Hi @patrick_dm I’m here again :face_with_head_bandage: trying to get a function in this date picker… What i’m trying to do is to set the 14th of the subsequent month as a due date, so and data submitted after that day should give a popup that submission is late before user can proceed. i had something working, but problem is my function works if today is equal to that due date I set. When tomorrow comes, it doesn’t work… What could i be doing wrong ? below is a snippet of the code

Hello @Beats, please do not post images for code, but paste you snippet as text between two lines containing three back-ticks, like:

```
// your code here
var foo = “bar”;
var now = DateTime.now()
```

so it will be formatted and color highlighted:

// your code here
var foo = "bar";
var now = DateTime.now()

@patrick_dm apologies thanks…

Future _chooseDate(BuildContext context, String initialDateString) async {
    DateTime liabilityDate = DateTime.now();
    var now = new DateTime.now().subtract(Duration(days: 30));
    var formatter = new DateFormat('yyyy-MM-dd');
    var initialDate = convertToDate(initialDateString) ?? now;
    initialDate = (initialDate.year >= 2020 && initialDate.isBefore(now)
        ? initialDate
        : now);
    var result = await showRoundedDatePicker(
        context: context,
        initialDate: initialDate,
        firstDate: new DateTime(2019,DateTime.december),
        lastDate: new DateTime(now.year,now.month + 1, 0));
    var firstDayOfMonth = new DateTime(result.year, result.month, 1);
    var lastDayOfMonth = new DateTime(result.year, result.month + 1, 0);

    if (result == null) return;
    setState(() {
      if (_taxType == 'TOT' && (liabilityDate.day <= 14)  ) {
        if(firstDayOfMonth.month + 1 == liabilityDate.month){
          print(liabilityDate.day + 14);
          print(firstDayOfMonth.month < liabilityDate.month);
          taxPeriodStartDate.text = formatter.format(firstDayOfMonth);
          taxPeriodEndDate.text = formatter.format(lastDayOfMonth);
        } else{ showDialog(
            context: context,
            builder: (BuildContext context) {
              return MessageDialog(
                  title: 'Attention',
                  message:
                  'Please note that penalties will be charged for your late submission of this return!',
                  icon: Icons.cancel,
                  iconColor: Colors.red.shade500);
            });
        print(liabilityDate.day + 14);
        print(firstDayOfMonth.month < liabilityDate.month);
        taxPeriodStartDate.text = formatter.format(firstDayOfMonth);
        taxPeriodEndDate.text = formatter.format(lastDayOfMonth);
        }
      }

I’m not getting the overall logic, sorry, but I guess that with this

if (_taxType == 'TOT' && (liabilityDate.day <= 14) ) { ...

the subsequent

if (firstDayOfMonth.month + 1 == liabilityDate.month) {...} else {...}

will not be evaluated when liabilityDate.day (given that liabilityDate has been assigned a DateTime.now() ) is greater than 14.
So today, July 15th, you should not get any printed values or alert dialog, nor tomorrow and until August 1st.

1 Like

Okay so what i want to check is the date today in any given month. If it is before or equal to the 14th, then the dialog should not pop for the previous month since it’s due date has passed, but any other previous month, it should popup. If it is after the 14th, it should popup for all previous months. I hope I’m making sense.

Well, I still have problem figuring out the requirements. I’d suggest you to write down a flow-chart diagram, or a pseudo code algorithm to analyse the problem before translating it into code. It will be easier for you to spot faults, and others will be able to reason about it too. :sweat_smile:

1 Like