I have 2 String in which I have hours and min
String1 = 2 HOUR 0 MIN
String2 = 1 HOUR 30 MIN
I need to check if I subtract String2 time with String1 values to go to negative or not.
For example, if I subtract String2 with String1 value will be in a time like 00:30
So basically I just need to check String2 is not greater then String1, I am badly stuck on this how can i check it
Is there a reason you're using Strings instead of Durations? The duration class has built in methods to add and subtract hours, mins, etc.
If you have to use strings, I would first convert them to Durations and add/subtract them.
I would consider first converting the Strings to Duration by parsing them. This can be done with a regexp for example:
/// Returns the duration associated with a string of
/// the form "XX HOUR XX MIN" where XX can be 1 or 2 digits
///
/// TODO: add check for fail safe
Duration _parseDateString(String dateString) {
// Make sure that this is correct, it really depends on the form of your input string
final dateStringRegexp = RegExp(r'(\d*) HOUR (\d*) MIN');
final match = dateStringRegexp.firstMatch(dateString);
final hours = int.parse(match!.group(1)!);
final minutes = int.parse(match.group(2)!);
return Duration(hours: hours, minutes: minutes);
}
Once you have this, it's pretty easy to compare the times:
final dateString1 = "2 HOUR 0 MIN";
final dateString2 = "1 HOUR 30 MIN ";
final duration1 = _parseDateString(dateString1);
final duration2 = _parseDateString(dateString2);
print(duration1.compareTo(duration2));
Related
For Example: Sunset-Sunrise.org provides sunset/sunrise time with HH:MM:SS format.
Given a time such as 12:53:57, I want to round the seconds to 12:54:00. Please advise.
A general technique for rounding is to add half of the unit you want to round to and then truncating. For example, if you want to round an integer to the nearest ten's digit, you can add 5 and discard the one's digit: ((x + 5) ~/ 10) * 10.
The same technique works for times too. You can first parse the HH:MM:SS string into a DateTime object. Then, to round the DateTime to the nearest minute, you can add 30 seconds and copy all of the resulting fields except for the seconds (and subseconds):
DateTime roundToMinute(DateTime dateTime) {
dateTime = dateTime.add(const Duration(seconds: 30));
return (dateTime.isUtc ? DateTime.utc : DateTime.new)(
dateTime.year,
dateTime.month,
dateTime.day,
dateTime.hour,
dateTime.minute,
);
}
You can use date_time_fromat packages
from the docs
final timeOffset = dateTime.subtract(Duration(hours: 6, minutes: 45));
// 7 hours
print(DateTimeFormat.relative(timeOffset));
// 6 hours
print(DateTimeFormat.relative(timeOffset, round: false));
This is the URL
I have a string "HH:mm" and I want to find the diff between that string to now time in seconds.
For instance, if the time now is 2021-02-24 18:00:00.000000 and my string is "00:30" then the result should be 23400 seconds.
How do I do that in dart?
Thanks!
Updated answer to make it match your question better.
Okey, given the answers as I understood from the comments, here is a suggestion. You can of course make it less expressive and combine the rows.
final now = DateTime.parse('2021-02-24 18:00:00.000000');
String time = '00:30';
final dateFormat = DateFormat('yyyy-MM-dd');
final todayString = dateFormat.format(now);
String stringToParse = '$todayString $time:00';
final parsedDateTime = DateTime.parse(stringToParse);
final timeDifference = parsedDateTime.difference(now);
int seconds = timeDifference.inSeconds > 0
? timeDifference.inSeconds
: timeDifference.inSeconds + 86400;
print(seconds.toString());
Will print 23400
I am trying to convert a textfield input of military time into a double. Can anyone help me with this? goal would be if someone enters 13:45 then the output would be 13.75.
Divide your input into 2 halves around the :. Then parse each half, which results in separate hours and minutes ints. Add them together, dividing the minutes by 60 to get your intended double output.
String input = "13:45";
String firstHalf = input.substring(0, input.indexOf(':'));
String secHalf = input.substring(input.indexOf(':') + 1);
int hour = int.parse(firstHalf);
int min = int.parse(secHalf);
double output = hour + min/60;
print(output);//13.75
Currently I get a number from the BE that states like 750 minutes (int) and should represent the time of day.
750 minutes = 12.5 hours and so the UI should display 12:30
1080 minutes = 18 hours and so the UI should display 18:00
But I can't seem to find a way to get a clean convertion from minutes to a proper Object with hours and minutes.
In the end I want to have a TimeOfDay object
I will continue to struggle and will also post if I find the answer myself :)
With the help of the comments above I resolved it by splitting the sting. I don't see this as the best way to convert since I'm dependened that the Duration Class will never change but never the less I got what I wanted by doing the following
TimeOfDay minutesToTimeOfDay(int minutes) {
Duration duration = Duration(minutes: minutes);
List<String> parts = duration.toString().split(':');
return TimeOfDay(hour: int.parse(parts[0]), minute: int.parse(parts[1]));
}
Create Duration with the amount of minutes, then toString() that instance this will outpout 00:00 and then split this string on the colon ":"
I suggest doing two mathematical operations like this:
TimeOfDay minutesToTimeOfDay(int minutesPastMidnight) {
int hours = minutesPastMidnight ~/ 60;
int minutes = minutesPastMidnight % 60;
return TimeOfDay(hour: hours, minute: minutes);
}
This is roughly based on this answer to a related question.
I have 2 times stored in character arrays in MATLAB.
a = '11:00 PM'
b = '07:30 AM'
I want to find the difference in hours between the 2 times, which should be 8.5 hours in this example. Is there any short method to do that? I can datenum both numbers, subtract them, datevec the difference, extract the hours and minutes from the vector, and convert them into hours, but this takes a lot of lines. Is there a more efficient way of doing this or is there an existing function?
You can do this by converting each string using datetime, taking the difference, then converting the result with hours:
numHours = hours(diff(datetime({a; b}, 'InputFormat', 'hh:mm a')));
numHours = numHours + 24.*(numHours < 0)
numHours =
8.5000
The second line accounts for the condition in your example, where the second time has to occur on the next day for the time difference to be positive, so 24 hours are added to the (negative) difference.
add a date to the time
like
a = '1/1/2000 11:00 PM'
b = '1/1/2000 07:30 AM'
the convert the string to datetime
x=str2num(strrep(a,':',''))
y=str2num(strrep(b,':',''))
then fine the difference between 2 dates
e = etime(x,y)
this will give you number of seconds between both times