Datetime to string format issue

조회 수: 9 (최근 30일)
Dharmesh Joshi
Dharmesh Joshi 2022년 11월 19일
답변: Dharmesh Joshi 2022년 11월 20일
Hi
I have a datetime array as shown below.
timearray_segmenta =
struct with fields:
start: [2022-09-06T00:00:00 2022-09-16T00:00:00 2022-09-26T00:00:00 2022-10-06T00:00:00 2022-10-16T00:00:00 2022-10-26T00:00:00 ]
stop: [2022-09-15T23:59:59 2022-09-25T23:59:59 2022-10-05T23:59:59 2022-10-15T23:59:59 2022-10-25T23:59:59 2022-11-04T23:59:59 ]
The formate of the date time has to be as shown above, this was achived using:
timearray.Format = 'yyyy-MM-dd''T''HH:mm:ss';
I need to convert this datetime structure as a string, so i am using the following:
K>> datestr(timearray_segmenta.start)
ans =
7×11 char array
'06-Sep-2022'
'16-Sep-2022'
'26-Sep-2022'
'06-Oct-2022'
'16-Oct-2022'
'26-Oct-2022'
'05-Nov-2022'
The formate changes, if i was to reassign the formate i get the following:
datestr(timearray_segmenta.start,timearray.Format )
ans =
7×21 char array
'2022-00-06'T'00:09:00'
'2022-00-16'T'00:09:00'
'2022-00-26'T'00:09:00'
'2022-00-06'T'00:10:00'
'2022-00-16'T'00:10:00'
'2022-00-26'T'00:10:00'
'2022-00-05'T'00:11:00'
What needs to change in my code?
  댓글 수: 1
Stephen23
Stephen23 2022년 11월 19일
"What needs to change in my code?"
Get rid of deprecated and superfluous DATESTR. Just set the DATETIME format:

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

채택된 답변

Steven Lord
Steven Lord 2022년 11월 19일
Don't use datestr. As its documentation page states, it is not recommended. Use string or char instead.
dt = datetime('now');
dt.Format = 'yyyy-MM-dd''T''HH:mm:ss'
dt = datetime
2022-11-19T13:28:00
s = string(dt)
s = "2022-11-19T13:28:00"

추가 답변 (2개)

dpb
dpb 2022년 11월 19일
>> datetime(start,'InputFormat','yyyy-MM-dd''T''HH:mm:ss')
ans =
datetime
06-Sep-2022
>> datetime(start,'InputFormat','yyyy-MM-dd''T''HH:mm:ss','Format','yyyy-MM-dd''T''HH:mm:ss')
ans =
datetime
2022-09-06T00:00:00
>> string(ans)
ans =
"2022-09-06T00:00:00"
>>

Dharmesh Joshi
Dharmesh Joshi 2022년 11월 20일
Thanks
String(), seems to have worked perfectly?

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by