Convert date and time in dart [duplicate] - flutter

This question already has answers here:
How do I convert a date/time string to a DateTime object in Dart?
(8 answers)
How do I format a date with Dart?
(24 answers)
Closed 9 months ago.
from API I am getting this:
"2022-05-12T15:55:03.000000Z"
My question is, how can I make it:
12.05.2022, 15:55
My question is, is here any easy way? Otherwise I can slowly and partially convert it with string methods, but I guess there must be better, more proper way?
Thank you in advance

you can user DateFormat to get a date as string formatted the way you want
String x = "2022-05-12T15:55:03.000000Z";
DateTime y = DateTime.parse(x);
final DateFormat formatter = DateFormat('dd.MM.yyyy, HH:mm');
final String formatted = formatter.format(y);
print(formatted); // 12.05.2022, 15:55

Related

Date format for the following dates? [duplicate]

This question already has answers here:
How do I format a date with Dart?
(24 answers)
Closed 4 months ago.
I want to show a date time in the following format 01-Mar-2000 10:10:10 PM/AM.
How to achieve it?
Please suggest a one line answer to write optimized code.
You can use intl package DateFormat.
The format will be DateFormat("dd-MM-yyyy h:mm:ss a");
single line will be
DateFormat("dd-MM-yyyy h:mm:ss a").format(dateTime);
final dateTime = DateTime.now();
DateFormat formater = DateFormat("dd-MM-yyyy h:mm:ss a");
final data = formater.format(dateTime);
print(data);//30-10-2022 4:14:19 PM

Format date into a month count and years

We have a DateTime value returning from JSON its coming thru as "10/9/2016 4:46:48 PM" .
What we need to do with it is format it to months or years past like so:
10/9/2016 = 3 years in the past.
The value 10/20/2019 = 3 months
Is this possible?
I'm guessing we would need to grab the month and year and subtract from today's date.
So I would create a function which will calculate difference between today's date and DateTime passed to it. It would look like this
String calculateDifference(DateTime dateTime) {
String text = "months";
double difference = DateTime.now().difference(dateTime).inDays / 30;
if (difference > 11) {
difference = difference / 12;
text = "years";
}
return "${difference.toStringAsFixed(0)} $text";
}
So you just need to parse the date from your JSON to DateTime object and pass it to a variable. You can also add one more condition to return value for days
You can use .difference on DateTime
final dateInThePast = DateTime(2018, 1, 7);
final dateNow = DateTime.now();
final difference = dateNow.difference(dateInThePast).inDays;
And then calculate from Days to Months / Years

Issue in converting string to double [duplicate]

This question already has answers here:
swift: issue in converting string to double
(2 answers)
Closed 3 years ago.
I have mobile bank app.
When user type amount, then convert string to double i have problem
user typed amount example "8.7" is 8.699999999999999 and when i send request it sending 8.699999999999999
what can i do, to fix it?
I have tried this post:
swift: issue in converting string to double
var amount = "8.7"
var amountDouble = Double(amount)!
var amount = "8.7" . //"8.7"
var amountDouble = Double(amount)! //8.699999999999999
This imprecision is exactly why Double isn't an appropriate datatype for financial domains. Use Decimal instead, which is have perfect precision within its legal range.

How to work with date in Unity c#? [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 4 years ago.
I create a game in unity. And I add a 'buy subscribe' (apple).
After click buy, I get Date (for example: 08/02/2018 07:24:23) 08 - is month, 02 - is date.
I get a date through this code:
productReceipt.subscriptionExpirationDate
Now i need to do a check, if CurrentDay - 08/02/2018 07:24:23 = 15 days;
How to calculate 2 dates?
You can do it using DateTime function. DateTime function allows you to calculate the number of days. You just need to get current datetime & parse your string to datetime format. Here is the code.
DateTime dateCurrent = DateTime.Now;
DateTime dateNext = DateTime.Parse(date);
int days = (dateNext - dateCurrent).Days;
Update:
You can also use TotalDays function to give the remaining time in fraction also. TotalDays is double so remember to change the days to double when using it.

number formatter : I need 01 not 1 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I pad an integer on the left with zeros?
In my iPhone app I am calculating time in this manner.
int timeTakeInSeconds = (int)(self.timeTaken*[constants timer]);
int timeTakenMins= (int)(timeTakeInSeconds/60);
int timeTakenSecs =(int)(timeTakeInSeconds%60);
NSString *timeText = [NSString stringWithFormat:#"Time %d:%d",timeTakenMins,timeTakenSecs];
By this I am getting second as 1,2,...10, 11..and so on. But I need as 01, 02, 03...10, 11, 12..and so on..How can I do this? Is there any way to do this by number formatter? Please help.
Try using %d:%02d instead of just %d:%d. This is using the standard C-style printf formatting options. The documentation can be found at Format Specifiers.