Flutter: Display local datetimes correctly - flutter

let's say I have a DateTime object and I want to display it in the correct local format.
If I do the following on a German device I get this:
dateTime.toLocal().toString()
// Prints
2022-05-28 23:29:19.518
However, I would expect or desire more something like this for a German device: 28.5.2022 23:29:19
I know that I can format the DateTime but that would just be hardcoding it for a certain locale.
Weirdly enough all the solutions that I found for this on StackOverflow are either hardcoding the format or only apply to Dart, not Flutter.
What is the correct way to display a local datetime in Flutter?

You can use this package intl and localise dates like
var format = DateFormat.yMd('ar');
var dateString = format.format(DateTime.now());

Using the intl package which was mentioned here already, this has been working well for me so far:
DateFormat dateTimeFormat = DateFormat.jm(Localizations.localeOf(context).toString());
DateTime dt = DateTime.fromMicrosecondsSinceEpoch(entity.syncDateTime);
dateTimeFormat.format(dt);
To get outputs which are not yet supported I, for example, concat a ymd formatted DateTime string with a jm formatted DateTime string.

Related

DateFormat dateFormat = DateFormat("yyyy-MM-dd"); To Month-Day-Year

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

Get correct date from excell on flutter

Hello guys I'm having a little hard time figuring this out.
So I have a small excell that I'm putting some information there(adding and requesting)
The problem is when I'm trying to get the date as string, and add it as DateTime.
Always the same error "Invalid Date Format"-
I have my dates on excell, as Simple Text saved as "20-06-2022", and displaying that on flutter with "user[index].date", all ok. The problem is that I want to compare the dates with a random day.
I've tried
DateTime.parse(users[index].date); // not working
Text(users[index].date); // not working ( shows random numbers as 44734)
The DateTime.parse method only accepts specific formats that are listed in the documentation.
Since yours is not one of those, you need to create a DateFormat instance of your own and use that one to parse
void main() {
final text = '20-06-2022';
final format = DateFormat('dd.MM.yyyy');
final date = format.parse(text);
print(date);
}

How can I get only date and time value from timestamp value in flutter

I am developing a Flutter project and I want to get 'timestamp' data from firestore and display that value using 'yyyy-MM-dd hh:mm' format. How can I do it ?
Firestore will give you back a Timestamp object as I think you've figured out. It has a method .toDate() which will return a dart DateTime object.
Once you have the Date object, then you should use the DateFormat class as someone so helpfully pointed out in a comment without even linking to the documentation.
That would look something like this:
DateTime date = timestamp.toDate();
DateFormat(yyyy-MM-dd hh:mm").format(date);
although I haven't tested out the format function and dartpad doesn't seem to support the intl package, so you may need to play around with the format codes a bit.

using DateFormat to produce a "2016-12-28T17:43:47.345Z"

I have been looking at the above question and have most of it correct.
I am going to get a datetime in Zulu, and then will want to output that format.
My first go is just as simple as:
DateFormat format = new DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
My issue I am having is the T and Z. Obviously T is used to separate the date from the time and the Z is representative of Zulu time.
That being said the users will be entering a datetime in Zulu, so it wont need to be converted from Local to Zulu, so i was not sure if 'Z' is an acceptable result. I was not sure if there is a different want to handle this, or if my result was the best answer.
Try this package, Jiffy.
String isoFomart = Jiffy().format(); // This will return ISO format from now
You can also add your DateTime object
String isoFomart = Jiffy(DateTime.now()).format(); // This will also return ISO format from now
Hope this helped
The DateTime object has a method called: toIso8601String which is used to return an ISO formatted string. The 'Z' will be added if isUTC is true, otherwise the result will not have the Z in it.
Make sure that the DateTime object itself is correctly set to UTC as if you look in the constructor for the class will tell you a lot of the defaults are local with the exception of the DateTime.utc() static function.
In that concept, you dont really need a DateFormat use to define an iso string.

Why does ExtJS subtract a day when formatting a date?

Using ExtJS 4.0.2, I can type the following into the console:
Ext.util.Format.date('2012-01-13', "m-d-Y");
I get 01-12-2012
Why?
I can correct it with:
Ext.util.Format.date('2012-01-13 00:00:00', "m-d-Y");
Ext.util.Format.date in Ext 4.0.2 uses a Date object or a String (your case). This string is parsed using the native Date.parse() using the UTC time zone.
Try to explicitly parse it using Ext.Date.parse:
var dt = Ext.Date.parse("2012-01-13", "Y-m-d");
Ext.util.Format.date(dt, "m-d-Y");
This problem exists in Ext3, but the solution is slightly different:
var dt = '2012-01-31'; //date string
dt = Date.parseDate(dt, "Y-m-d");
Ext.util.Format.date(dt, 'm/d/Y'); //returns 01/31/2012
If you're unable to use Gregor's answer (e.g. filling a grid), note that changing the input to a non ISO 8601 date format will avoid the UTC parsing as well. For example
Ext.util.Format.date('01/13/2012', "Y-m-d");
will give 2012-01-13