Advice needed with the dateshift function

I'm struggling with the dateshift function and need some help if anyone can offer it.
I need to write some code that, once a menu option is chosen, will provide the next of two dates. i.e. If option 1 is selected on the menu, then either the next sunday date is displayed or the next wednesday date, whichever is nearest.
I can display the next sunday date, AND the next wednesday date, but i can't display the next sunday OR the next wednesday.

1 件のコメント

Michael T
Michael T 2019 年 4 月 12 日
Here is the code i have been using. I'm not an expert so i understand there may be an easier way to do this.
t = datetime('today');
t = datetime;
if Menu == 1
Sunday = dateshift(t,'dayofweek','Sunday',1)
end
if Menu == 2
Wednesday = dateshift(t,'dayofweek','Wednesday',1)
end
if Menu == 3
Next_Day_Is = dateshift(t,'dayofweek','Sunday','Wednesday',1)
end

サインインしてコメントする。

回答 (3 件)

Walter Roberson
Walter Roberson 2019 年 4 月 12 日

0 投票

days_to_shift_Sw = [0 2 1 0 3 2 1]
days_to_shift_Sw( day(t, 'dayofweek') )

4 件のコメント

Michael T
Michael T 2019 年 4 月 13 日
Hi Walter,
Thanks for your help. I’m not quite sure how to implement the code you sent though. I can see by running it that a 1 appears, presumably because it’s Sunday tomorrow so the result is shifted one day. But I’m not sure how to adapt that to the core I sent originally?
Walter Roberson
Walter Roberson 2019 年 4 月 13 日
Add that number of days to the current date.
Question though: if it is already Sunday, then should the date stay the same, or should it be the next Wednesday ?
Michael T
Michael T 2019 年 4 月 13 日
If it’s already Sunday then the date can stay Sunday. It’s really more if it’s monday or Thursday for example.
Walter Roberson
Walter Roberson 2019 年 4 月 13 日
Okay, so just add the indicated number of days to the current day.

サインインしてコメントする。

AMAN GARG
AMAN GARG 2020 年 7 月 27 日

0 投票

dayshift(x,'Start','Hour')
Steven Lord
Steven Lord 2020 年 7 月 27 日

0 投票

N = datetime('today')
W = dateshift(N, 'dayofweek', 'Wednesday')
S = dateshift(N, 'dayofweek', 'Sunday')
answer = min(W, S)

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

タグ

質問済み:

2019 年 4 月 12 日

回答済み:

2020 年 7 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by