필터 지우기
필터 지우기

String date and time identification

조회 수: 8 (최근 30일)
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2023년 1월 5일
댓글: Stephen23 2023년 1월 5일
Hello, I have the following string:
date= '2021-07-011407404.5'
From here I would like to detect date and time in the following formar: 2021-07-01 14:07:40, the last number (e.g. 4.5) is another data and this number it can vary like 16.50, and so on.
Thank you for your support

채택된 답변

Walter Roberson
Walter Roberson 2023년 1월 5일
datetime() has no ability to have a format item along the lines of "arbitrary number to be ignored". You cannot specify anything along the lines of 'yyyy-MM-ddHHmmss%*f' with the intent being that after the ss field that an arbitrary floating point number is to be expected but discarded. It does not even have the ability to specify that a particular fixed numeric digit is to be expected but ignored
datetime('2021-07QA', 'InputFormat', "uuuu-MM'QA'") %works
ans = datetime
01-Jul-2021
datetime('2021-075A', 'InputFormat', "uuuu-MM'5A'") %nope
Error using datetime
Unable to convert '2021-075A' to datetime using the format 'uuuu-MM'5A''.
Because of this, you need to remove those extra digits from the input before you call datetime(), such as using indexing as shown by @Star Strider and @Cameron

추가 답변 (3개)

Star Strider
Star Strider 2023년 1월 5일
편집: Star Strider 2023년 1월 5일
If they all have the same format —
date= '2021-07-011407404.5'
date = '2021-07-011407404.5'
DT = datetime(date(1:16), 'InputFormat','yyyy-MM-ddHHmmss', 'Format','yyyy-MM-dd HH:mm:ss')
DT = datetime
2021-07-01 14:07:40
LastNumber = str2double(date(17:end))
LastNumber = 4.5000
They must all have the same formats for this to work.
EDIT — (5 Jan 2023 at 18:28)
Added 'Format' name-value pair to datetime call.
.
  댓글 수: 1
Stephen23
Stephen23 2023년 1월 5일
An alternative to indexing would be to use EXTRACTBEFORE(), which also works on string/cell of char arrays.

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


Cameron
Cameron 2023년 1월 5일
편집: Cameron 2023년 1월 5일
How consistent are is the information in your variable called date? Will they always look like what you have described in your question? You should consider renaming the variable from date to something that is not already a MATLAB function like DateValue.
DateValue = '2021-07-011407404.5';
NewDate = DateValue(1:10);
NewTime = [DateValue(11:12),':',DateValue(13:14),':',DateValue(15:16)];
RemainingInfo = str2double(DateValue(17:end));
%from here you can make them into formats MATLAB recognizes as datetime
DateTimeValue = datetime([NewDate,' ',NewTime]);

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 1월 5일
Hi,
Here is one of the possible ways of displaying the time strings:
DD= '2021-07-011407404.5';
DDate = DD(1:10)
DDate = '2021-07-01'
D2 = DD(11:16);
DTime = strcat([D2(1:2) ':' D2(3:4) ':' D2(5:6)])
DTime = '14:07:40'
D3 = DD(17:end);
DTime2 = strcat([D3(1) '.' D3(end)])
DTime2 = '4.5'
ALL = ['Date: ' DDate ' Time: ' DTime ' Seconds: ' DTime2]
ALL = 'Date: 2021-07-01 Time: 14:07:40 Seconds: 4.5'

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by