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
Related
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));
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'm trying to convert int values to minutes, and in some cases it gives me stars * in the result:
string "U"$"99:59" / "99:59"
string "U"$"100:00" / "**:00"
What are the rules for this stars to appear?
Update for seconds:
string "V"$"1000" / "10:00:00"
string "V"$"10000" / "**:00:00"
string "V"$"100000" / "10:00:00"
string "V"$"1000000" / "**:00:00"
Is it possible to configure this rules, say to stop smudging the origin values?
I think the stars that appear are just kdb's way of saying that the result couldn't be displayed.
The minute and second datatypes have the form hh:mm and hh:mm:ss, respectively. So if kdb reads the hour part as having more than 3 digits (i.e greater than 99), the stars will appear.
If you're casting a string into a minute datatype, then kdb converts the last two chars into minutes, and the rest into hours, eg
"U"$"1234" / 12:34
"U"$"12345" / **:45
Something similar occurs when you convert a string into seconds. If the length of the string is 6 or greater, then the last two chars are converted into seconds. Otherwise, the seconds are set to 00. The rest of the string is then converted into hh:mm as above. To illustrate this, look at:
"V"$"1234" / 12:34:00
"V"$"12345" / **:45:00
"V"$"123456" / 12:34:56
"V"$"1123456" / **:34:56
I should also note that even though stars appear, you can still do things like temporal arithmetic as usual, eg
"U"$"100:00 / **:00
("U"$"100:00")-"U"$"1:00" / 99:00
Is there a method in matlab to convert seconds from a known date to a standard date time format?
For example, if I have a vector of values shown as seconds from 1901/01/01, how would I convert them to a dateTime? In this case a value of 28125 would correspond to 1981/01/01. Is there an efficient method for doing this?
The numbers in your example do not make sense so it is not clear if your time is in seconds or days but since you asked for seconds I will use this.
What you want to achieve can be done using datenum function. This function returns the number of (fractional) days from 1/1/0000. So first you need to find your offset, e.g.:
offsetInDays = datenum(1901,1,1);
Next, you convert the date from seconds to days:
dateInDays = YourRequiredDateInSec * 3600 * 24;
Finally, you date is given by
RequiredDate = datestr(offsetInDays + dateInDays);
I have some time as string format in my data. Can anyone help me to convert this date to milliseconds in Matlab.
This is an example how date looks like '00:26:16:926', So, that is 0 hours 26 minutes 16 seconds and 926 milliseconds. After converting this time, I need to get only milliseconds such as 1576926 milliseconds for the time that I gave above. Thank you in advance.
Why don't you try using datevec instead? datevec is designed to take in various time and date strings and it parses the string and spits out useful information for you. There's no need to use regexp or split up your string in any way. Here's a quick example:
[~,~,~,hours,minutes,seconds] = datevec('00:26:16:926', 'HH:MM:SS:FFF');
out = 1000*(3600*hours + 60*minutes + seconds);
out =
1576926
In this format, the output of datevec will be a 6 element vector which outputs the year, month, day, hours, minutes and seconds respectively. The millisecond resolution will be added on to the sixth element of datevec's output, so all you have to do is convert the fourth to sixth elements into milliseconds and add them all up, which is what is done above. If you don't specify the actual day, it just defaults to January 1st of the current year... but we're not using the date anyway... we just want the time!
The beauty with datevec is that it can accept multiple strings so you're not just limited to a single input. Simply put all of your strings into a single cell array, then use datevec in the following way:
times = {'00:26:16:926','00:27:16:926', '00:28:16:926'};
[~,~,~,hours,minutes,seconds] = datevec(times, 'HH:MM:SS:FFF');
out = 1000*(3600*hours + 60*minutes + seconds);
out =
1576926
1636926
1696926
One solution could be:
timeString = '00:26:16:926';
cellfun(#(x)str2num(x),regexp(timeString,':','split'))*[3600000;60000;1000;1]
Result:
1576926
Assuming that your date string comes in that format consistently, you could use something as simple as this:
test = '00:26:16:926';
H = str2num(test(1:2)); % hours
M = str2num(test(4:5)); % minutes
S = str2num(test(7:8)); % seconds
MS = str2num(test(10:12)); % milliseconds
totalMS = MS + 1000*S + 1000*60*M + 1000*60*60*H;
Output:
1576926.00
you can convert a single string with a date or even a vector by using datevec for conversion and the dot product
a = ['00:26:16:926' ; '08:42:12:936']
datevec(a,'HH:MM:SS:FFF') * [0 0 0 3600e3 60e3 1e3]'
ans =
1576926
31332936