I have the time in datetime format like below. I would like to extract the time from '20-Apr-2020 11:20:10' till '20-Apr-2020 12:40:50'. Do I need it to convert it first to datenumber or I can do it directly here?
Time_datenum={'20-Apr-2020 11:06:00','20-Apr-2020 11:20:10','20-Apr-2020 11:45:30','20-Apr-
2020 12:07:00','20-Apr-2020 12:35:40','20-Apr-2020 12:40:50','20-Apr-2020 13:07:00'};
Time_datetime = datetime(Time_One,'InputFormat','dd-MM-yyyy HH:mm:ss');
Time_datenum={'20-Apr-2020 11:06:00','20-Apr-2020 11:20:10','20-Apr-2020 11:45:30',...
'20-Apr-2020 12:07:00','20-Apr-2020 12:35:40','20-Apr-2020 12:40:50','20-Apr-2020 13:07:00'};
% Create a datetime array from a cell array of character vectors.
Time_datetime = datetime(Time_datenum, 'InputFormat', 'dd-MM-yyyy HH:mm:ss', 'Locale', 'en_GB');
% t = datetime(Year, Month, Day, Hour, Minute, Second)
Time_start = datetime(2020, 4, 20, 11, 20, 10);
Time_end = datetime(2020, 4, 20, 12, 40, 50);
% Extract the time.
Time_extracted = Time_datetime(Time_start <= Time_datetime & Time_datetime <= Time_end);
Related
For example, if times is given as 1.05 seconds, we need to convert it as 00:01:05.
How can we achieve this in Flutter?
Also, if a time is given as duration in milliseconds, how do we convert it to the 00:00:00 format?
You just need some simple integer maths to separate the duration into the units you want and some string formatting to make sure that single-digit numbers get leading zeros.
String formatForVideo(Duration d) {
final millis = d.inMilliseconds;
if (millis >= 3600000) {
throw FormatException('too big to format');
}
final minutes = _pad2(d.inMinutes);
final seconds = _pad2(d.inSeconds % 60);
final cents = _pad2((millis % 1000) ~/ 10);
return '$minutes:$seconds.$cents';
}
String _pad2(int i) => i.toString().padLeft(2, '0');
Which gives expected results:
print(formatForVideo(Duration(minutes: 0, seconds: 1, milliseconds: 50))); // 00:01.05
print(formatForVideo(Duration(minutes: 9, seconds: 51, milliseconds: 50))); // 09:51.05
print(formatForVideo(Duration(minutes:60))); // exception
The output of this definition => 00:00:00
DateTime time = DateTime(1990, 1, 1, 0, 0, 0, 0, 0);
Then when you want to add a period to this time
time.add(const Duration(seconds: 100,milliseconds:200))
and for output =>
String timeString = DateFormat.Hms().format(time);
print(timeString);
Result =>> 00:01:40
What data type is used in dart instead of the TimeSpan data type in C#?
It is Duration.
For example:
void main(List<String> args) {
// Define two dates.
DateTime date1 = new DateTime(2010, 1, 1, 8, 0, 15);
DateTime date2 = new DateTime(2010, 8, 18, 13, 30, 30);
// Calculate the interval between the two dates.
Duration interval = date2.difference(date1);
print('$date2 - $date1 = $interval');
// Display individual properties of the resulting TimeSpan object.
print('Total Number of Days: ${interval.inDays}');
print('Total Number of Hours: ${interval.inHours}');
print('Total Number of Minutes: ${interval.inMinutes}');
print('Total Number of Seconds: ${interval.inSeconds}');
print('Total Number of Milliseconds: ${interval.inMilliseconds}');
}
the result will be:
2010-08-18 13:30:30.000 - 2010-01-01 08:00:15.000 = 5501:30:15.000000
Total Number of Days: 229
Total Number of Hours: 5501
Total Number of Minutes: 330090
Total Number of Seconds: 19805415
Total Number of Milliseconds: 19805415000
I need to create a list of datetime as presented in the attached Figure in Matlab, but I am not getting it.. so far I have done:
t1 = datetime(2016, 4, 1, 0, 0, 0);
t2 = datetime(2018, 12, 31, 23, 45, 0);
tinc = 15;
t = t1:minutes(tinc):t2;
But it gives me "01-Apr-2016 00:00:00"
Instead, I need:
2016-04-01 00:00:00
2016-04-01 00:15:00
...
2018-12-31 23:45:00
Can someone explain to me how can I do it?
(The +01:00 is for daytime savings days that I can append later I guess).
Use the Format option with the desired specifier:
>> t= datetime(t1:minutes(tinc):t2,'Format','yyyy-MM-dd HH:mm:SS');
>> t(1:2)
ans =
1×2 datetime array
2016-04-01 00:00:00 2016-04-01 00:15:00
You can find more information on the Display Format for datetime here.
How to get the closest timestamp to now from List?
I got a List of timestamps and I want to determine the closest timestamp in the future to current timestamp.
How can I achieve that?
Something like this? I am not sure how you are representing your timestamps so I have made the example by using DateTime objects:
void main() {
final dateTimes = <DateTime>[
DateTime(2020, 8, 1),
DateTime(2020, 8, 5),
DateTime(2020, 7, 13),
DateTime(2020, 7, 18),
DateTime(2020, 8, 15),
DateTime(2020, 8, 20)
];
final now = DateTime(2020, 7, 14);
final closetsDateTimeToNow = dateTimes.reduce(
(a, b) => a.difference(now).abs() < b.difference(now).abs() ? a : b);
print(closetsDateTimeToNow); // 2020-07-13 00:00:00.000
}
Note, the solution finds the closets timestamp in the list and looks both in the past and future.
I am trying to get the difference in Days between two dates picked from a DatePicker. This works fine except for ONE single date : March 31.
The difference in Days between two DateTimes is wrong by 1 day when one of the dates is March 31. I know this is due to Light Saving and March is 30.9… days long and not 31, hence I am guessing, the error. But does anyone know how to fix this other than manually checking if a date is equal to March 31 and adding one day to the result ?
Two very simple examples that can be run in the Dart Pad :
DateTime aprilFirst = DateTime(2019, 3, 30);
DateTime marchThirtyFirst = DateTime(2019, 3, 31);
print(aprilFirst.difference(marchThirtyFirst).inDays); => -1
DateTime marchThirty = DateTime(2019, 4, 1);
DateTime marchThirtyFirst = DateTime(2019, 3, 31);
print(marchThirty.difference(marchThirtyFirst).inDays); => 0
UPDATE:
DateTime aprilFirst = DateTime(2019, 4, 1);
print(aprilFirst.add(Duration(days: -1))); => 2019-03-30 23:00:00.000
This should print 2019-03-31 23:00:00.000 !
I tried Günter Zöchbauer's solution of making the DateTimes UTC but the results are the exact same:
DateTime aprilFirst = DateTime(2019, 4, 1).toUtc();
DateTime marchThirty = DateTime(2019, 3, 30).toUtc();
DateTime marchThirtyFirst = DateTime(2019, 3, 31).toUtc();
print(aprilFirst.difference(marchThirtyFirst).inHours); => 23
print(aprilFirst.difference(marchThirtyFirst).inDays); => 0
print(marchThirty.difference(marchThirtyFirst).inHours); => -24
print(aprilFirst.add(Duration(days: -1))); => 019-03-30 22:00:00.000Z
#Günter Zöchbauer put me on the right path. DateTime(...).toUTC() will fail for difference calculations. However, using the DateTime.utc(...) constructor does the trick !
DateTime aprilFirst = DateTime.utc(2019, 4, 1);
DateTime marchThirty = DateTime.utc(2019, 3, 30);
DateTime marchThirtyFirst = DateTime.utc(2019, 3, 31);
print(aprilFirst.difference(marchThirtyFirst).inHours); => 24
print(aprilFirst.difference(marchThirtyFirst).inDays); => 1
print(marchThirty.difference(marchThirtyFirst).inHours); => -24
print(aprilFirst.add(Duration(days: -1))); => 2019-03-31 00:00:00.000Z
Don't do Date comparison or operations with local dates. Convert it to UTC first. Otherwise daylight savings and other local DateTime related exceptions will cause all kinds of surprising effects.
DateTime aprilFirst = DateTime(2019, 3, 30).toUtc();
DateTime marchThirtyFirst = DateTime(2019, 3, 31).toUtc();
print(aprilFirst.difference(marchThirtyFirst).inDays); => -1
If the result is a DateTime you can convert it back using xxx.toLocal()
There is also a constructor that allows to create an UTC DateTime instead of creating a local DateTime and then converting to UTC.
Try this package, Jiffy uses the momentJs concept of date-time difference
You can see dicussion here https://github.com/moment/moment/pull/571
DateTime aprilFirst = DateTime(2019, 3, 30);
DateTime marchThirtyFirst = DateTime(2019, 3, 31);
Jiffy(aprilFirst).diff(marchThirtyFirst, Units.DAY); // -1
// or
Jiffy([2019, 3, 30]).diff([2019, 3, 31], Units.DAY); // -1
DateTime marchThirty = DateTime(2019, 4, 1);
DateTime marchThirtyFirst = DateTime(2019, 3, 31);
Jiffy(marchThirty).diff(marchThirtyFirst, Units.DAY); // 1
Jiffy(marchThirty).diff(marchThirtyFirst, Units.HOUR); // 24
Jiffy(marchThirty).diff(marchThirtyFirst, Units.MONTH); // 0
2021 Based on answer
extension DateCalcs on DateTime {
DateTime get dateOnlyUTC => DateTime.utc(this.year,this.month,this.day);
}
You've already figured out that you get an unexpected result due to your locale observing Daylight Saving Time. Duration internally stores a number of microseconds; Duration.days is measured in terms of 24-hour increments and not in terms of calendar days. From the DateTime documentation:
The difference between two dates in different time zones is just the number of nanoseconds between the two points in time. It doesn't take calendar days into account. That means that the difference between two midnights in local time may be less than 24 hours times the number of days between them, if there is a daylight saving change in between. If the difference above is calculated using Australian local time, the difference is 7415 days and 23 hours, which is only 7415 whole days as reported by inDays.
See https://stackoverflow.com/a/71198806/ for a more general version of Benjamin's answer that computes the difference in days between two dates, ignoring the time (and therefore also ignoring Daylight Saving adjustments and time zones).