필터 지우기
필터 지우기

datetime() does not return time data

조회 수: 10 (최근 30일)
Emily T. Griffiths
Emily T. Griffiths 2022년 10월 26일
댓글: Steven Lord 2022년 10월 27일
Hello all,
I just noticed a bug in my code that my datetime function is not converting time data from the character string.
My code is the following:
DTstart = datetime("2021-11-16 11:48:00", "InputFormat","yyyy-MM-dd HH:mm:ss");
This has worked on all versions of matlab without fail. However, I just updated to 2022b. Now when I run this, instead of getting:
DTstart =
datetime
16/11/2021 11:48:00
I get simply:
DTstart =
datetime
16/11/2021
This is causing a HUGE headache in my code. I need to look at thing by the second. It doesn't matter if I run the text as a string or a character. Everytime I try to search for an alternative, I just get redirected to the datetime function. Can someone please explain to me why this function is no longer working, or perhaps suggest an alternative?

채택된 답변

Steven Lord
Steven Lord 2022년 10월 26일
In the Preferences, expand the MATLAB item in the tree. Select the Command Window item. What does it list for the default datetime display formats? Did you accidentally change the default?
  댓글 수: 2
Emily T. Griffiths
Emily T. Griffiths 2022년 10월 27일
Oh my goodness. Thank you so much. I wasn't aware this could change. Like I said, I just updated, so perhaps I clicked something during the install. Relieved to know the data is still there!
Steven Lord
Steven Lord 2022년 10월 27일
Even if you'd changed the display format, the data was still present in the variable.
DTstart = datetime("2021-11-16 11:48:00", ...
"InputFormat","yyyy-MM-dd HH:mm:ss", ...
'Format', 'yyyy-MM-dd')
DTstart = datetime
2021-11-16
You can see this by asking for the time of day stored in DTstart.
timeofday(DTstart)
ans = duration
11:48:00

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

추가 답변 (1개)

Voss
Voss 2022년 10월 26일
Try specifying the Format (in addition to the InputFormat):
DTstart = datetime("2021-11-16 11:48:00","InputFormat","yyyy-MM-dd HH:mm:ss",'Format','dd/MM/yyyy')
DTstart = datetime
16/11/2021
DTstart = datetime("2021-11-16 11:48:00","InputFormat","yyyy-MM-dd HH:mm:ss",'Format','dd/MM/yyyy HH:mm:ss')
DTstart = datetime
16/11/2021 11:48:00
  댓글 수: 1
Emily T. Griffiths
Emily T. Griffiths 2022년 10월 27일
I had tried this, but as seen above, it was a display issue. :)

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by