datetime to string conversion

조회 수: 2 (최근 30일)
alpedhuez
alpedhuez 2022년 3월 12일
댓글: Star Strider 2022년 3월 12일
I have
day = datetime(2008,04,01)
with the output
day =
datetime
01-Apr-2008
I then run
string(day)
with the output
ans =
"01-Apr-2008"
But I want to have a string "20080401." Is there any simple way to do this?

채택된 답변

Star Strider
Star Strider 2022년 3월 12일
Ise the 'Format' name-value pair —
dayv = datetime(2008,04,01, 'Format','yyyyMMdd')
dayv = datetime
20080401
string(dayv)
ans = "20080401"
.
  댓글 수: 2
alpedhuez
alpedhuez 2022년 3월 12일
편집: alpedhuez 2022년 3월 12일
It is true, But I now see that the problem I posted above actually now comes from the issue that
datelimits = datetime({'20080401', '20080402'}, 'InputFormat', 'yyyyMMdd');
outputs
datelimits =
1x2 datetime array
01-Apr-2008 02-Apr-2008
Although it should output
20080401 20080402
Please advise how to fix this issue so that it would output in the correct format.
Star Strider
Star Strider 2022년 3월 12일
Again, use 'Format'
datelimits = datetime({'20080401', '20080402'}, 'InputFormat', 'yyyyMMdd', 'Format','yyyyMMdd')
datelimits = 1×2 datetime array
20080401 20080402
.

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

추가 답변 (0개)

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by