Convert a datetime to string

조회 수: 1,348 (최근 30일)
Lieke van Boxtel
Lieke van Boxtel 2023년 11월 22일
댓글: Star Strider 2023년 11월 23일
I want to convert a specific datetime from a table into a string with a certain format, which it also uses in the table. I tried using datestring, with a format for the output : checkdate = datestr(datelist{1,d},'yyyy_MM_dd')
When I use this format for the table I get: 2020_07_17
But when using it with the datestr I get this: '22-Feb-2022'. How can I get the same format from the table as a string?
  댓글 수: 5
Walter Roberson
Walter Roberson 2023년 11월 23일
The datestr call does produce character vectors based upon a variety of ways of inputting times. However, it is based on an older Mathworks time implementation that Mathworks calls "serial date numbers", which are "number of days and fraction of days since the beginning of year 0 CE." For example,
format long g;
now
ans =
739213.334071768
so were are currently approaching 3/4 of a million days since the beginning of 0 CE.
The time resolution available with serial date numbers is roughly 10 microseconds.
This is not the only way that MATLAB has of representing time. Mathworks added what it calls "datetime objects" and "duration objects". Datetime objects internally use two double precision objects together, datetime objects have a resolution better than seconds.
If you have a datetime object and you want to convert it to a character vector with a particular format, first set the Format property of the object to the format you want, and then char() or string() the datetime object.
extime = datetime('July 17, 2020 15:43:19')
extime = datetime
17-Jul-2020 15:43:19
string(extime)
ans = "17-Jul-2020 15:43:19"
extime.Format = 'yyyy_MM_dd';
string(extime)
ans = "2020_07_17"
Stephen23
Stephen23 2023년 11월 23일
"I want to convert a specific datetime from a table ... But when using it with the datestr I get this: '22-Feb-2022'"
dt = datetime(2022,2,22) % curiously many 2's
dt = datetime
22-Feb-2022
datestr(dt,'yyyy_MM_dd') % does not match what you say you got
ans = '2022_00_22'
If you used DATESTR with that format then there is no way you could get a month abbreviation from uppercase M's.
If you used the correct format (as Alexander Paul showed) then you could:
datestr(dt,'yyyy_mmm_dd') % does not match what you say you did
ans = '2022_Feb_22'
"When I use this format for the table I get: 2020_07_17"
If the data in the table is not really a DATETIME but really text then note that underscores are not part of any of the supported syntaxes for input date strings. Lets see what happens anyway:
datestr('2022_07_17','yyyy_mmm_dd')
ans = '2022_Jul_17'
Well, DATESTR seems to have coped with that just fine. Without uploading sample data it is unclear what the problem is.

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

채택된 답변

Star Strider
Star Strider 2023년 11월 22일
I am not certain what the problem is.
To convert [2022 07 17] to your desired format using datetime, try this —
DT = datetime([2022 07 17], 'Format','yyyy_MM_dd')
DT = datetime
2022_07_17
To convert it to a string variable:
DTstr = string(DT)
DTstr = "2022_07_17"
and to a character array:
DTchr = sprintf('%s', DT)
DTchr = '2022_07_17'
.
  댓글 수: 2
Lieke van Boxtel
Lieke van Boxtel 2023년 11월 23일
Thanks, doing seperate steps using string did the trick for me.
Star Strider
Star Strider 2023년 11월 23일
As always, my pleasure!

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2023년 11월 22일
if you have a datetime object then set its Format property. In some cases that is all you need to do; in other cases you need to string() or char() the datetime object afterwards.

Alexander
Alexander 2023년 11월 22일
Use mm instead of MM.
checkdate = datestr(datelist{1,d},'yyyy_mm_dd')
should do the work. As I have not your datelist, I haven't tested it.
  댓글 수: 4
Alexander
Alexander 2023년 11월 23일
You mixing up datestr and datetime! With datestr you have to use mm for months and MM for minutes.
Lieke van Boxtel
Lieke van Boxtel 2023년 11월 23일
Ah, thanks, yes I was confusing those. Weird that it is different in this case, but good to know.

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

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by