How to convert DateTime to Duration in flutter? - flutter

I have trouble to convert input like 72:46:00.15768 to duration like 4366 minutes
I tried this but return null after trying the .inMinutes()
var _lastConso = lastConsoString3!=null ? DateTime.parse(lastConsoString3) : DateTime.now();
Diff = DateTime.now().difference(_lastConso);
DurationDiff=Diff.inMinutes;

Try this
/// 72:46:00.15768
var lastConsoString3= Duration(hours:72,minutes: 46,seconds: 00,milliseconds: 15768);
var _lastConso =DateTime.now().subtract(lastConsoString3);
var diff = DateTime.now().difference(_lastConso);
print( diff.inMinutes); /// 4366 your answer

Maybe your variable that named lastConsoString3 not well formatted for DateTime. Below code works fine. And you need to write var or exact type of variable.
var _lastConso = DateTime.now().add(Duration(days: -12)); //your parsed date
var diff = DateTime.now().difference(_lastConso);
var durationDiff = diff.inMinutes;

Related

how to subtract two different times in flutter

I want to subtract two different times in flutter for example
String time1 = "07:00";
String time2 = "08:12";
String time3 = time2 - time1;
//result will be 01:12
this is just a sample data explanation of what I want to achieve.
See the sample code below. It prints 01:12 in the end.
Input Data:
String time1 = "07:00";
String time2 = "08:12";
Required Methods to be defined:
DateTime getTime(final String inputString) => DateFormat("hh:mm").parse(inputString);
String getString(final Duration duration) {
String formatDigits(int n) => n.toString().padLeft(2, '0');
final String minutes = formatDigits(duration.inMinutes.remainder(60));
return "${formatDigits(duration.inHours)}:$minutes";
}
Computation
final String difference = getString(getTime(time2).difference(getTime(time1)));
Printing the result
print(difference); // 01:12
Have fun - but keep in mind to change the naming of the methods to better fit into your context.
You should use Dart's built-in DateTime class and its DateTime.parse especially.
Documentation: https://api.dart.dev/stable/2.8.4/dart-core/DateTime/parse.html
Try out below code for get difference between two times.
var timeFormat = DateFormat("HH:mm"); //Time format
var first = timeFormat.parse("10:40");
var second = timeFormat.parse("18:20");
print("Difference -->${second.difference(first)}");
// prints Difference -->7:40
Flutter|Dart provides a data type to handle dates : DateTime. So instead of using String, use DateTime :
DateTime time1 = DateTime.parse(your_date_here your_time);
DateTime time2 = DateTime.parse(your_date_here your_time);
DateTime time3 = time2.difference(time1);
If you want just the time part, you can format it using DateFormat from the intl package. You'll need to import it as follows :
import 'package:intl/intl.dart';
Then do the following:
String formattedTime = DateFormat.Hms().format(time1);
String formattedTime = DateFormat.Hms().format(time2);
Or you can just directly format the result (time3):
String formattedTime = DateFormat.Hms().format(time3);

Trying to read yy from 2022/04/17 at position 0 - in flutter

This is how I used the time and date output format,
To get time on this item "getCartListHistory [itemCounter] .time"
Widget getDataTime(){
DateTime parseData = DateFormat('yy/MM/dd HH:mm:ss').parse(getCartListHistory[itemCounter].time!);
var inputDate = DateTime.parse(parseData.toString());
var outPutFormat = DateFormat.yMd().add_jm();
var outputDate = outPutFormat.format(inputDate);
return BigText(text: outputDate);
}
try pass also the time when parsing it will read same pattern on DateFormat
DateTime parseData = DateFormat('y/MM/dd HH:mm:ss').parse("2022/04/17 09:24:00");
var inputDate = DateTime.parse(parseData.toString());
var outPutFormat = DateFormat.yMd().add_jm();
var outputDate = outPutFormat.format(inputDate);
log(outputDate.toString());
result :
4/17/2022 9:24 AM

Compare two Dates in SAPUI5

I want to compare, whether Date A is greater than Date B. But I always get false, even if Date A is greater.
var oDatepicker = this.getView().byId("Date");
var oFormat = sap.ui.core.format.DateFormat.getInstance({ pattern: "d.M.y" });
var oDate = oFormat.format(new Date());
var oDatepickerParsed = oFormat.parse(oDatepicker.getValue());
if(oFormat.format(oDatepickerParsed) > oDate){
return true;
} else {
return false;
}
I tried to instantiate a Date-Object based on oDatepicker.getValue() to compare Date-Object with Date-Object, but there is something wrong.
var oDateObject = new Date(oDatepicker.getValue())
oDatepicker.getValue() is = '01.11.2020' type string. Whats wrong?
Did you try the DatePicker method getDateValue() which gives you "the date as JavaScript Date object. This is independent from any formatter."

Changing Pentaho date values to first and last day of the week in Javascript Modified Value

I am trying to set two variables to the first and last day of the week for a given date, but the .setDate() method does not seem to be changing the date and 'lastday' and 'firstday' variables return an Invalid Date (1970)
DateNew is from my input step which is defined as dd/MM/yyyy format
var curr = DateNew;
var first = getDayNumber(curr,"d") - getDayNumber(curr,"wm")
var last = first + 7;
var firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();
Declaring the input variable as a date seemed to resolve the issue as there was no javascript type associated with it.
var curr = new Date(DateNew);
var first = getDayNumber(curr,"d") - getDayNumber(curr,"wm")
var last = first + 7;
var firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();

how to convert string to time formatted in flutter

I have data from database "192624". how I can change the String format in flutter become time formatted. example "192624" become to "19:26:24". I try using intl packages is not my hope result.
this my code
DateTime inputDate = inputDate;
String formattedTime = DateFormat.Hms().format(inputDate);
in above is not working
I want result convert data("192624") to become "19:26:24". data time from database.
use this method
String a() {
var a = "192624".replaceAllMapped(
RegExp(r".{2}"), (match) => "${match.group(0)}:");
var index = a.lastIndexOf(":");
a = a.substring(0,index);
return a;
}
Have you checked out this a answer :
String time;
// call this upper value globally
String x = "192624";
print(x.length);
x = x.substring(0, 2) + ":" + x.substring(2, 4)+":"+x.substring(4,x.length);
time =x;
print(x);
just globally declare the string and then assign the local String to global then call it in the ui