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
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));
I am attempting to convert the following 2HR time in String to a DateTime, so that I manage it appropriately.
"1800"
This question came close, but no one answered how to convert the String into a valid DateTime.
How to convert time stamp string from 24 hr format to 12 hr format in dart?
Attempt 1:
DateTime.parse("1800") -
Invalid Date Format
Attempt 2:
DateTime.ParseExact("1800") -
This doesn't seem to exist, although it shows up on various
Still no luck and need a second pair of eyes to point out the obvious to me.
The time by itself is not a datetime so you could do something like:
DateTime myTime(DateTime baseDate, String hhmm) {
assert(hhmm.length == 4, 'invalid time');
final _hours = int.parse(hhmm.substring(0, 2));
final _mins = int.parse(hhmm.substring(2, 2));
return DateTime(baseDate.year, baseDate.month, baseDate.day, _hours, _mins);
}
DateTime.parse expects to parse dates with times, not just times. (You can't even create a DateTime object without a date!)
Some people generate a dummy date string, but in your case you could trivially parse it with int.parse and then apply appropriate division and remainder operations:
var rawTime = int.parse('1800');
var hour = rawTime ~/ 100;
var minute = rawTime % 100;
Also see How do I convert a date/time string to a DateTime object in Dart? for more general DateTime parsing.
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
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
Iam reading out a Json file with a date in it.
jsonValue->GetObject()->GetNamedObject("board")->GetNamedNumber("date")
That date is saved in Unix code format:
"date":1347973494
But I need to get it in a normal format like "19.09.2012".
I cant find the right function to solve that problem.
I already tried the DateTimeFormatter class but I think that was not the correct way to make this.
So anyone knows how to change the DateTime from Unix timestamp to a normal format like "19.09.2012"?
A Unix timestamp is seconds since 1970, so add the seconds to 1970-01-01.
int unixTimestamp = 1347973494;
System::DateTime timestamp = System::DateTime(1970, 1, 1).AddSeconds(unixTimestamp);
Then format the DateTime into whatever string format you like, or use it as a DateTime.
System::String^ formatted = timestamp.ToString("dd.MM.yyyy")
I solved the problem with the Calendar class which you can find here
int unixTimestamp = (int)jsonValue->GetObject()->GetNamedNumber("date");
Windows::Globalization::Calendar^ cal = ref new Windows::Globalization::Calendar();
cal->Year = 1970;
cal->Month = 1;
cal->Day = 1;
cal->Minute = 0;
cal->Hour = 0;
cal->Second = 0;
cal->AddSeconds(unixTimestamp);
mainDate -> Text = cal->DayAsString() + ". " + cal->MonthAsString() + " " + cal->YearAsString();