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.
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.
CodePudding user response:
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
CodePudding user response:
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);