Add time to datetime format
조회 수: 32 (최근 30일)
이전 댓글 표시
Hi,
I have a datetime format in the following format (MM-dd-yyyy), but it does not have time. How can I add time to it for instance 00:00 (midnight) so I get the format MM-dd-yyyy HH:mm:ss.
댓글 수: 0
채택된 답변
Star Strider
2020년 8월 23일
Try this:
de = ['08/21/2020 (Aug)'; '09/21/2020 (Sep)']
DT = datenum(de, 'mm/dd/yyyy')
DS = datestr([DT + zeros(size(DT,1),1)], 'mm/dd/yyyy HH:MM:SS')
producing:
DS =
2×19 char array
'08/21/2020 00:00:00'
'09/21/2020 00:00:00'
or alternatively just.
DS = datestr(DT, 'mm/dd/yyyy HH:MM:SS')
producing the same result.
.
댓글 수: 0
추가 답변 (1개)
Steven Lord
2020년 8월 23일
편집: Steven Lord
2020년 8월 23일
A datetime array generally has a time component, even if it's not shown[*].
>> dt = datetime('today')
dt =
datetime
23-Aug-2020
>> dt.Format = 'dd-MMM-yyyy hh:mm:ss a'
dt =
datetime
23-Aug-2020 12:00:00 AM
[*] From the documentation for datetime: "Datetime values later than 144683 years CE or before 140743 BCE display only the year numbers, regardless of the specified Format value."
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!