problem with dateshift..how solve it?

1 view (last 30 days)
Luca Re
Luca Re on 20 Oct 2023
Commented: Luca Re on 21 Oct 2023
data1=datetime("2023-01-01")
data1 = datetime
01-Jan-2023
data2=datetime("2023-11-15")
data2 = datetime
15-Nov-2023
dt=data1:days(7):data2
dt = 1×46 datetime array
Columns 1 through 14 01-Jan-2023 08-Jan-2023 15-Jan-2023 22-Jan-2023 29-Jan-2023 05-Feb-2023 12-Feb-2023 19-Feb-2023 26-Feb-2023 05-Mar-2023 12-Mar-2023 19-Mar-2023 26-Mar-2023 02-Apr-2023 Columns 15 through 28 09-Apr-2023 16-Apr-2023 23-Apr-2023 30-Apr-2023 07-May-2023 14-May-2023 21-May-2023 28-May-2023 04-Jun-2023 11-Jun-2023 18-Jun-2023 25-Jun-2023 02-Jul-2023 09-Jul-2023 Columns 29 through 42 16-Jul-2023 23-Jul-2023 30-Jul-2023 06-Aug-2023 13-Aug-2023 20-Aug-2023 27-Aug-2023 03-Sep-2023 10-Sep-2023 17-Sep-2023 24-Sep-2023 01-Oct-2023 08-Oct-2023 15-Oct-2023 Columns 43 through 46 22-Oct-2023 29-Oct-2023 05-Nov-2023 12-Nov-2023
RP=dateshift(data1:calmonths(1):data2, 'end', 'month');
RP = dateshift(RP,'dayofweek','Saturday','previous');
RP(end)
ans = datetime
25-Nov-2023
why RP(end) is > data2 ?
i write: dt=data1:days(7):data2 the result must to be <=data2 but it's > data2

Answers (2)

Walter Roberson
Walter Roberson on 20 Oct 2023
dt=data1:days(7):data2 does end before data2 -- the last entry is the 12-Nov-2023 that you see displayed.
But then you take something that starts at the beginning of the month of January 2023, and advance one calendar month at a time ending no later than data2 . The beginning of Novemeber 2023 is several calendar months after January 1 2023, so the data1:calmonths(1):data2 series ends with a November 1 2023 datetime object. you then dateshift() that to the end of the month, getting a November 30, 2023 datetime object. You then dateshift() that to the previous Saturday, which gets you the November 25 2023 datetime object. There is no reason why the last Saturday of a month must be before a datetime that is mid-month.
  7 Comments
Stephen23
Stephen23 on 21 Oct 2023
Most likely FIND is not required, logical indexing is simpler and more efficient:
RP(RP<dt(end))

Sign in to comment.


Mann Baidi
Mann Baidi on 20 Oct 2023
Hi Luca,;
I understnad you are facing issues in getting the desired output using the "dateshift" function. You are encountering the above issue because when the code is executed, the array "RP" has the end value as "30-Nov-2023" after the execution of line 4.
data1=datetime("2023-01-01");
data2=datetime("2023-11-15");
dt=data1:days(7):data2;
RP=dateshift(data1:calmonths(1):data2, 'end', 'month')
RP = 1×11 datetime array
31-Jan-2023 28-Feb-2023 31-Mar-2023 30-Apr-2023 31-May-2023 30-Jun-2023 31-Jul-2023 31-Aug-2023 30-Sep-2023 31-Oct-2023 30-Nov-2023
RP = dateshift(RP,'dayofweek','Saturday','previous');
RP(end);
This is the reason "RP(end)" is giving "25-Nov-2023" as it is the previous saturday for "30-Nov-2023".
However, if you would like to avoid this issue and would like to find the previous Saturday of "data2" you can modify the code by passing "RP(end-1)" instead of "RP(end)" in line 5 of the code., and then check for the "data2"separately. Here is the possible workaround.
data1=datetime("2023-01-01");
data2=datetime("2023-11-15");
dt=data1:days(7):data2;
RP=dateshift(data1:calmonths(1):data2, 'end', 'month');
RP = dateshift(RP(end-1),'dayofweek','Saturday','previous');
RP(end)
ans = datetime
28-Oct-2023
lastSat= dateshift(data2,'dayofweek','Saturday','previous')
lastSat = datetime
11-Nov-2023
Hope this will resolve your doubt and query!
  1 Comment
Luca Re
Luca Re on 20 Oct 2023
thank but i thik i code this:
RP(find(RP<dt(end)))
ans =
1×10 datetime array
Columns 1 through 7
2023-01-28 2023-02-25 2023-03-25 2023-04-29 2023-05-27 2023-06-24 2023-07-29
Columns 8 through 10
2023-08-26 2023-09-30 2023-10-28

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!