Analyze Data for last complete Calendar week prior to current date

2 次查看(过去 30 天)
I am at basic level in Coding with Matlab, will very much like my function to return the start and end-dates of the last full calendar week prior to today's date (full Calendar week from Sunday - Saturday). For example; if today is Monday, 20th Sep 2021, the function should return:
Start_Weekdate = '12-Sep-2021' and End_Weekdate = '18-Sep-2021'.
I unfortunately do not know how to define the appropriate Calendar Weekdate reference
function AnalyseWeekInterval(Home, START_Weekdate, END_Weekdate)
%prepare Tiem frame to analyse
START_Weekdate= datetime(FIRSTWEEKDAYDATE, 'TimeZone','local','InputFormat','dd-mmm-yyyy');
END_Weekdate= datetime(LASTWEEKDAYDATE, 'TimeZone','local','InputFormat','dd-mmm-yyyy');
end
I will appreciate your expertise on this.
Thanks in Advance

采纳的回答

Siddharth Bhutiya
Siddharth Bhutiya 2021-9-21
There might be a couple of ways to do this, but the function that you are looking for is dateshift.
>> dt = datetime(2021,9,20); % Our date of interest
>> End_Weekdate = dateshift(d,'dayofweek','Saturday','previous') % Get the date of the previous Saturday using dateshift
End_Weekdate =
datetime
18-Sep-2021
>> Start_Weekdate = End_Weekdate - caldays(6) % Subtract 6 calendar days from it to get the date for the start of the week
Start_Weekdate =
datetime
12-Sep-2021
  1 个评论
Lambert Desa
Lambert Desa 2021-10-1
@Siddharth Bhutiya: perfect, I am truely grateful for your solution, its concise and efficient. I really should think less complicated sometimes.
Regards,
Lambert

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by