-2

DateFormat dateFormat = DateFormat("yyyy-MM-dd"); This line of code will not let me change the DateFormat to ("MM-dd-yyyy") without showing throwing an error in the app, or not displaying correctly.

I just need it to show MONTH-DAY-YEAR, NOT YEAR-MONTH-DAY

Flutter refuses to let me change it. Nobody seems to have an answer for how to change it. I've been looking for two weeks on how to change it with no luck.

Also asked here: Flutter DatePicker change format to MM-DD-YYYY, but none of the answers worked either.

Here's the error being thrown in the app:

enter image description here

EDIT: More Code Context.

The original line was this:

DateFormat dateFormat = DateFormat("yyyy-MM-dd");

DateTime dateTime = dateFormat.parse(ticketModel.date);

dateTime was then getting shown via Text in a container this way:

 child: Text(dateTime.toString().split(' ').first.toString(),
                style: Theme.of(context).textTheme.bodyMedium!.copyWith(
                      letterSpacing: 2.5,
                      color: Colors.white,
                    )),

This is NOT my code, and I am not a Flutter dev. Something is happening in these lines that won't allow MM-dd-yyyy to work properly.

The error is a photo, because it is getting displayed in the app container, not in any sort of terminal. I didn't want any mistakes if I tried copying it to text. I know its frowned upon, this isn't my first rodeo getting scolded on Stackoverflow, but I was desperate here.

My issue was solved for now by following that person's answer below and changing my code to this:

 DateFormat dateFormat = DateFormat("yyyy-MM-dd");

 DateTime fudgeThis = dateFormat.parse(ticketModel.date);

 String dateTime = fudgeThis.showDateInOwnFormat();

Hopefully this edits help someone. Seriously.

  • What precisely is the error message? – jraufeisen Sep 25 '22 at 20:01
  • Exactly what did you try to generate that error? You should be using `DateFormat('MM-dd-yyyy')`, but the error indicates that you tried to use `MMMM` somewhere in your format string, which is not a valid format specifier. `M` corresponds to month, `d` to day, and `y` to year. The number of each letter sometimes indicates the number of digits or sometimes is used to indicate that you want names instead of numbers, but `MMMM` makes no sense. – jamesdlin Sep 26 '22 at 02:23
  • [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Ken White Sep 26 '22 at 02:25
  • The error message says `MMM`, not `MM`. It appears you have a typo in your actual code, which is clearly not the code that you typed in here. In the future, **always** copy and paste your **actual** code, as re-typing it here can make it different from the code that is actually causing the problem. – Ken White Sep 26 '22 at 02:29
  • 1
    Sorry Ken, I can't copy the error from anywhere. It's actually displayed as the container image on the app. If you know how to pull the error from the container and paste it here, please share. –  Sep 26 '22 at 02:31
  • @PowermasterPrime Again: what is the code you tried? `DateFormat('MM-dd-yyyy')` should work, and the error message indicates that it is NOT what you have tried. – jamesdlin Sep 26 '22 at 02:38
  • @PowermasterPrime If you don't believe me: https://dartpad.dev/?id=5a0258982833916d6f911395ca127910 – jamesdlin Sep 26 '22 at 03:06

2 Answers2

0

let's say I have the DateTime variable

DateTime ourDateExample = DateTime.now();

you can create your own extension on DateTime to achieve what you want

extension ShowDataInOwnFormat on DateTime {
  String showDateInOwnFormat() {
  return '$month-$day-$year';
}}

put this outside of your classes or on a separate file, then call it whenever you want like this :

ourDateExample.showDateInOwnFormat();

it will return the date as the Month-day-year

Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
  • Would you know an appropriate place to put that extension? I'm not familiar with the feature in Dart, so I'm wondering where it should be placed? This seems to be the closest to what I'm looking for, and I can't thank you enough. –  Sep 26 '22 at 02:37
  • `DateFormat('MM-dd-yyyy')` specifies a minimum number of digits for each date component. You would need to do `${month.toString().padLeft(2, '0')}` and so on to be equivalent. – jamesdlin Sep 26 '22 at 02:38
  • @PowermasterPrime The correct answer is `DateFormat('MM-dd-yyyy')`. Have you tried that? This answer will show dates in the form 1-2-2022, which is not the MM-dd-yyyy format you asked for. – jamesdlin Sep 26 '22 at 02:57
  • Hi,you can put it in any place just make sure to import it when you want to use it – Gwhyyy Sep 26 '22 at 03:02
  • yes, a padLeft will be useful – Gwhyyy Sep 26 '22 at 03:03
0

Thank you for finally showing your code. However, the code you showed is not the code that generated the error. The point of asking for the exact code that you tried is not to be rude or sassy; it's to allow other people to reproduce your problem and to determine its exact cause, such as whether your problem was caused by a typo or by some other mistake. As evident from the error, what you actually tried was MMMM instead of MM, which would be a typo.

Based on your code and on the error message, I also suspect that you accidentally reused the same DateFormat object for parsing and printing. That is, you probably tried something like:

DateFormat dateFormat = DateFormat("MMMM-dd-yyyy");

DateTime fudgeThis = dateFormat.parse(ticketModel.date);

String dateTime = dateFormat.format(fudgeThis); // WRONG

That would not work if you're trying to convert between formats. You instead should have used separate DateFormat objects:

DateTime fudgeThis = DateFormat("yyyy-MM-dd").parse(ticketModel.date);

String dateTime = DateFormat("MM-dd-yyyy").format(fudgeThis);

Unlike naively creating your own string from the month, day, and year components directly, the above will format the date with the specified minimum number of digits.

Additionally, your error message indicates that you were attempting to parse an ISO-8601 date string, so you probably don't need to use DateFormat for parsing anyway:

DateTime fudgeThis = DateTime.parse(ticketModel.date);

String dateTime = DateFormat("MM-dd-yyyy").format(fudgeThis);
jamesdlin
  • 81,374
  • 13
  • 159
  • 204