Flutter rounds DateTime value to 3 decimal places [duplicate] - flutter

This question already has an answer here:
DateTime.fromMillisecondsSinceEpoch not returning an exact DateTime object
(1 answer)
Closed last month.
I'm developing a web app using Flutter front-end and FastApi. I encountered this issue while using DateTime, that its value has a different length in either of the languages (Python & Dart).
DateTime object from python
2022-01-25T13:09:03.914910
DateTime object parsed from response
2022-01-25 13:09:03.915
I observed that Dart rounds the DateTime value to 3 decimal places.
How to get around this issue.

You're probably using the default toString() on the DateTime object. What you might need is the toIso8601String() method. Here's a code sample to show the difference.
final now = DateTime.now();
print(now); // 2022-01-25 11:04:08.053
print(now.toIso8601String()); // 2022-01-25T11:04:08.053

Related

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);
}

Flutter: Display local datetimes correctly

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.

Invalid date format [Flutter] [duplicate]

This question already has answers here:
How do I convert a date/time string to a DateTime object in Dart?
(8 answers)
Closed 9 months ago.
I've been trying convert a String to DateTime in dart using DateTime.parse(dateStart), where 'dateStart' is my variable that I use to get the date, for example in the next format '24/12/2022', but I catch invalid date format, I would like to known which are the validates formats to use in dart.
DateTime.parse(dateStart)
You need to use intl package to parse string date to datetime object. Please check sample Datetime parse example as below.
DateTime tempDate = new DateFormat("dd/MM/yyyy").parse(savedDateString);
You can format '24/12/2022' to DateTime like this:
final dateTime = DateFormat('dd/MM/yyyy').parse('24/12/2022');

What type of date string is this? `20210713T015433.354Z` [duplicate]

This question already has answers here:
What is this date format? 2011-08-12T20:17:46.384Z
(11 answers)
Closed 1 year ago.
I'm working with some data where I don't know how the time string was generated but an example of what the date string looks like is this 20210713T015433.354Z
I've looked at some examples of date strings (ex1 and ex2) but haven't had any luck decoding it.
I know that the first half of the string is YYYYMMDD, and then I think that T means times but then I don't know what 015433.354Z is.
My eventual goal is to make this into a Date objects in JS but I can't deduce the formatting. beyond the day it occured.
The date format provided is ISO 8601. Maybe you want to try the following to get a Date object:
new Date(Date.UTC(2021, 7, 13, 1, 54, 33, 354))
As pointed in this SO answer.

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.