Flutter - Convert minutes to TimeOfDay - flutter

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.

Related

Flutter compare two time string

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

Rounding seconds of time with HH:MM:SS format to nearest minute

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

How can i convert duration into hours only instead of days and hours using python 3 in jupyter notebook?

Here I am attaching the code screenshots.
my code
from datetime import datetime
filmpermits['StartDateTime'] = pd.to_datetime(filmpermits.StartDateTime)
filmpermits['EndDateTime'] = pd.to_datetime(filmpermits.EndDateTime)
filmpermits.head()
# filmpermits.dtypes
my code
duration = filmpermits.EndDateTime - filmpermits.StartDateTime
You can calculate this straight away when you calculate the duration. You just need to specify the type in hours:
duration = (filmpermits.EndDateTime - filmpermits.StartDateTime).astype('timedelta64[h]')
How can i convert duration into hours only instead of days and hours …
Your duration is a Series of pandas.Timedelta, which has a method total_seconds() from which we can compute the number of hours:
hours = duration.dt.total_seconds()/60/60

Convert integer number to time format

How can I convert an integer such as 115900 to a time? I'd like to do arithmetic operations on times so that something like: 115900 + 100 will equal 120000, rather than 11600.
Your big problem is that an integer number does not behave like a date/time. Since you are using Objective-C, you really should be using the NSDate class and the associated classes for formatting dates and times and managing calendars.
Start by reading the Date and Time Programming Guide. That will be better than me writing it all out again.
int seconds = 115900 % 60;
int minutes = (115900 / 60) % 60;
int hours = 115900/ 3600;
return [NSString stringWithFormat:#"%02i:%02i:%02i",hours, minutes, seconds];
//output like is HH:MM:SS

How to "round" minutes and hours

I did not really know on how to title this question so hopefully you've find the way in :)
My Problem is:
I wanted to set a clock time for a label with a UISlider.
So basically my slider min value is 0000 and the max value is 2400. (24 hour format)
So how do I achieve a properly formatted clock?
For example if my slider's value is at (1161)11:61 it should be (1201)12:01 and so on.
Any tipps for that :)
Would be great to get some help here.
Thanks to all who participate.
why don't you start from 0 to 1440. (24 hours = 1440 minutes) and do something like below.
int hours = slider.value / 60; -> no of hours;
int minutes = slider.value %60; -> no of minutes;
NSString *clock = [NSString stringWithFormat:#"%d : %d", hours, minutes];
You could do this with an NSDateComponents object. Create one, then break up your slider value into two parts: the thousands and hundred digits become the hour, and the tens and ones digits become the minute. You can feed this object to an NSCalendar to transform it into an actual NSDate (if that's what you want).