How to remove unnecessary numbers in DateTime - flutter

So, when I print DateTime.now() value, it shows those unnecessary numbers(on the screen the numbers are 323884). Is there any possibility on removing those numbers?
enter image description here

You need to use the DateFormat class.
First import intl package:
import 'dart:intl';
The use it like this:
print(DateFormat.yMd().format(new DateTime.now()));
You can use any format you'd like. See the docs for more formats

Let's Try This
import datetime
date=str(datetime.datetime.now())
date.split('.')[0]

Related

Format all the numbers on an app [Flutter]

Is there any way to format all the numbers on a flutter app?
By formatting I mean, for example, given a number (eg 4000) it’s shown on the UI as 4,000.
I know that there’s a way to do so using the intl package, but in that case, from what I know, I have to format each number individually and it’s not the most optimal thing to do in my case.
You can use NumberFormat passing a custom format in ICU formatting pattern, take a look in NumberFormat.
import 'package:intl/intl.dart';
void main() {
var formatter = NumberFormat('#,##,000');
print(formatter.format(16987));
print(formatter.format(13876));
print(formatter.format(456786));
}
Output
16,987
13,876
4,56,786

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.

DateFormat adding yy-mm-dd to DateTime when I specifically called for hh:mm

The code is as follows:
DateTime time = DateFormat("hh:mm").parse(doc[i]["Time"]);
The doc[i]["Time"] value is supposed to be a military time like 19:42. I followed the guidelines about using DateFormat but the end result variable time contains "1970-01-01 19:42:00.000". Is there a way for time to just contain "19.42"?
To get time in 24hrs format change your code to
String time = DateFormat("HH:mm").format(doc[i]["Time"]);
H -> is for 24 hours format
It is not possible to use DateTime to show a specific format, must always become a String.
A DateTime type will always store everything including year, month, hour, minutes, seconds, milliseconds, etc.
But when you later use the variable, you can chose what to display.
For example, you can chose to display just the military time. Here is one example using the Intl package (here)
Text(DateFormat('Hm').format(yourVariable))
Don't forget to import
import 'package:intl/intl.dart';

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.

Getting a GWT timestamp in UTC format

I am trying to get the current timestamp and convert it into a UTC date for an XML file.
I am using this
import java.util.Date;
import java.util.TimeZone;
import com.google.gwt.i18n.client.DateTimeFormat;
DateTimeFormat.format( new Date(), TimeZone.getTimeZone("UTC"));
but am getting the following error
The method format(Date, TimeZone) in
the type DateTimeFormat is not
applicable for the arguments (Date,
TimeZone)
I need the output as "yyyy-mm-ddThh:mm:ssZ"
Use com.google.gwt.i18n.client.TimeZone.createTimeZone(0) to create a UTC TimeZone object, and then use that in DateTimeFormat.format(Date, TimeZone).
You can use apostrophe to indicate literals in a DateTimeFormat pattern.
eg. "HH'o''clock'"
So, the formatter you need would look something like this:
DateTimeFormat formatter = DateTimeFormat.getFormat("yyyy-mm-dd'T'HH:mm:ssZ");
I tried it out. It gave me an output in format 2010-16-29T08:16:23+0530
Is this what you are looking for?
You should provide a com.google.gwt.i18n.client.TimeZone instead of java.util.TimeZone