Operate with date and time string

조회 수: 1 (최근 30일)
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2023년 1월 9일
Hello, I have the following strings as an examples that it will collected from many accelerometer records
dt_event= ''2021_04_23 16_58_14'
dt_site= ''2021_04_23 17_00_18'
I want to generate a condition from which compare these two strings and do not be higher that 50sec or 2min (this condition can be based on seconds and minutes of difference)
So the condition will be like
if abs(dt_event-dt_site)>=50sec (or 2min)
Results.Check = 1.
For some cases the differences between dt_event and dt_site will be more 5hours due to time convertion from local time and UTC time. But there will be some some seconds or 2min of difference too.
if abs(dt_event-dt_site)>=5hours + 50sec (or 2min)
How I can apply these condition iin this cases condidering that dt_site and dt_event are string dates and time converted with a specific format as shown.
Thank you.

채택된 답변

Vilém Frynta
Vilém Frynta 2023년 1월 9일
편집: Vilém Frynta 2023년 1월 9일
% Your datetime as strings
dt_event= '2021_04_23 16_58_14';
dt_site= '2021_04_23 17_00_18';
% Convert to datetime format
dt_event_NEW = datetime(dt_event,'InputFormat','yyyy_MM_dd HH_mm_ss');
dt_site_NEW = datetime(dt_site,'InputFormat','yyyy_MM_dd HH_mm_ss');
% Create durations for comparison
timeCompare1 = duration([5,2,0]); % 5hrs 2 minutes duration
timeCompare2 = duration([0,2,0]); % 2 minutes duration
timeDiff = abs(dt_event_NEW-dt_site_NEW) % difference between the times
timeDiff = duration
00:02:04
timeDiff > timeCompare1 % 2 mins > 5 hrs 2 mins = 0
ans = logical
0
timeDiff > timeCompare2 % 2 mins 4 seconds > 2 mins = 1
ans = logical
1
You can modify timeCompare as you wish, so you can compare the time difference to any other duration.
Hope I helped. Feel free to ask if you do not understand anything.
  댓글 수: 1
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2023년 1월 9일
Thank you very much. I tried some codings and it works perfectly. I will be back to you if I have any questions.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by