How to create modified date time in Matlab? - matlab

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.

Related

How to get dates by days which have intervals of 7 days each

hello guyz am new to flutter and am practicing on dates then i encounter a difficulty on how can i acheive the result same below using a loop or something in order to get this result.
so far i tried is
final now = DateTime.now();
for(var d = 1 ; d <= 5 ; d++){
// Don't know whats next to do here to get the same result below
}
i want result like this
Apr 19, 2022
Apr 26, 2022
May 3, 2022
May 10, 2022
May 17, 2022
can some help me and explain.
Dart's DateTime class has an add function, where you can specify a duration which will be added the current Date.
Therefore, what you can do is:
DateTime now = DateTime.now();
for(var d = 1; d <= 5; d++) {
...
now = now.add(Duration(days: 7)); // here you can specify your interval. Also possible: hours, minutes, seconds, milliseconds and microseconds
}
I'll recommend reading the Docs, e.g. DateTime Docs and Duration Docs

How to extract a specific timeframe from datetime?

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

Serial date number since "Jan 1, 0000": is this -1 BC or +1 CE?

I was working on dates in Matlab and Octave, and the "serial date number" format is documented as
A single number equal to the number of days since January 0, 0000 in the proleptic ISO calendar (specifying use of the Gregorian calendar).
In Octave, they document
Return the date/time input as a serial day number, with Jan 1, 0000 defined as day 1.
The gregorian calender does not use a year zero. But Matlab and Octave refer to the year zero. Does this mean that they refer to year -1 BC, as in the astronomical year numbering?
Days before "October 15, 1582" are "wrong by as much as eleven days", according to the octave manual, which is considerably smaller than a complete year. So I'm trying to sort out this ambiguity.
Firstly, note that the MATLAB and Octave definitions are equivalent
[MATLAB] N = "number of days since Jan 0, 0000" ⇔ [OCTAVE] "Jan 1, 0000 is day 1"
Since N = 1 on day 1.
The Wikipedia page on "Year Zero" (which you linked to) offers this:
[...] the year 1 BC is followed by AD 1. However, there is a year zero in astronomical year numbering (where it coincides with the Julian year 1 BC) and in ISO 8601:2004 (where it coincides with the Gregorian year 1 BC), as well as in all Buddhist and Hindu calendars.
MATLAB and Octave appear to have followed the ISO standard, as stated in the datetime docs:
datetime arrays represent points in time using the proleptic ISO calendar
So the year zero, and hence the datenum value of 1 days, coincides with the first day of 1BC.
Per the definitions at the top of this answer
"day 1"
= 1/Jan/0000
= datenum(1)
= datetime( 1, 'ConvertFrom', 'datenum' )
= datetime( 0, 0, 1 )
We can test using datenum (number of days) and datetime (datetime type object)
datenum( 0, 0, 1 ) % = 1, as defined by the docs
datetime( 1, 'ConvertFrom', 'datenum' )
% = 1/Jan/0000 00:00:00
datetime( 1 + 366, 'ConvertFrom', 'datenum' )
% = 1/Jan/0001 00:00:00
% First day of year 1 after 366 days (leap year 0000 + 1 for Jan 1 )

Dart/Flutter DateTime difference inDays error for March 31 April 1

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

how can i get the day of the week for a particular given date in Matlab

As today is Wednesday with date June 8, 2016. how can i write a code to get the day of given dates:
like what day is Nov 29
I'm trying to create a struct with
date
day
month
with month and date as input
Use the builtin weekday() function:
>> [num, name] = weekday('08-Jun-2016')
num =
4
name =
Wed
>> [num, name] = weekday('29-Nov-2016')
num =
3
name =
Tue
In addition to the weekday function, you can use the DDD or DDDD formats in the datestr function, like this:
datestr('08-Jun-2016','DDD') %Returns the string 'Wed'
datestr('08-Jun-2016','DDDD') %Returns the string 'Wednesday'
Or, to use a more practical format
datestr('08-Jun-2016','DDDD, mmmm DD, yyyy')
% Returns the string: 'Wednesday, June 08, 2016'