problem with dateshift..how solve it?
显示 更早的评论
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,'dayofweek','Saturday','previous');
RP(end)
why RP(end) is > data2 ?
i write: dt=data1:days(7):data2 the result must to be <=data2 but it's > data2
回答(2 个)
Walter Roberson
2023-10-20
1 个投票
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 个评论
shamal
2023-10-20
Walter Roberson
2023-10-20
You have given code, and expressed that the code does not do what you want, but you have not expressed what you do want?
Are you looking for the last saturday that is no later than data2 ?
Are you looking for the last saturday that is no later than dt(end) ?
Are you looking for the last saturday of a month that is no later than data2 ? If so then
dateshift(dateshift(data2, 'start', 'month') - 1, 'dayofweek', 'saturday', 'previous')
... but what do you want to do about the case where data2 is in the same calendar month as data1?
Hmmm... that dateshift line I gave is possibly wrong for the case where data2 is the last saturday of a month -- but whether it is wrong or not depends on your intention with the days(7) and calmonths(1) . For example if data1 is the monday of the last full week of November 2023, and data2 is the saturday of that week exactly, then the description "last Saturday of a month that is at least data1 and is no more than data2" would suggest data2 as the result -- but with your days(7) and calmonths(1) iterations, the only entry in the vectors would be data1 and applying the logic to that would lead you to the last saturday in October 2023 which would be before data1 in this case...
So you need to define clearly what exactly you are searching for.
shamal
2023-10-21
Walter Roberson
2023-10-21
I suspect you meant
RP(find(RP<dt(end)))
shamal
2023-10-21
Stephen23
2023-10-21
Most likely FIND is not required, logical indexing is simpler and more efficient:
RP(RP<dt(end))
shamal
2023-10-21
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 = 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)
lastSat= dateshift(data2,'dayofweek','Saturday','previous')
Hope this will resolve your doubt and query!
类别
在 帮助中心 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!