How do I convert this string to a date ?

Hi everybody !
I'm trying to turn this string to a date (from a txt file): 2021-05-11T14:11:00Z to 'yyyy-MM-dd HH:mm:ss'.
I tried with datetime(ref_time(h, 1),'InputFormat', 'yyyy-MM-dd e HH:mm:ss e','Format','yyyy-MM-dd HH:mm:ss') with an 'e' replacing the 'T' and 'Z' but id doesn't work...

답변 (2개)

Asmit Singh
Asmit Singh 2021년 5월 26일

2 개 추천

Since you are trying to convert strings to date with literals like 'T' and 'Z', you may want to look at the "Date and Time from Text with Literal Characters" section in the documentation.
The below code converts the given string format to matlab datetime variable.
myDate = "2021-05-11T14:11:10Z"
datetime(myDate,'InputFormat','yyyy-MM-dd''T''HH:mm:ss''Z')

댓글 수: 8

Allen
Allen 2021년 5월 26일
Asmit,
I just learned that the format for date strings can contain a text representation.
Thanks for sharing that approach.
Jules PASCO
Jules PASCO 2021년 5월 26일
It works !
The only problem is that I can't turn this format into a datetime, it stays a string.
Do you know how to turn 'May' to its month number '05' ?
Thanks
Stephen23
Stephen23 2021년 5월 26일
"The only problem is that I can't turn this format into a datetime, it stays a string."
The string you show in that screenshot does not match the format you described in your question.
Where did that string come from?
I am not sure how did you reach to this point. But you can still typecast these strings to datetime objects.
datetime("04-May-2021 11:00:00")
Jules PASCO
Jules PASCO 2021년 6월 1일
Stephen,
This picture comes from a simulation I did last week with Asmit's proposal. It allows me to go from 2021-05-04T11:00:00Z to 04-May-2021 11:00:00, which is already good. Now I would just like to change the format from 04-May-2021 11:00:00 to 04-05-2021 11:00:00, i.e. change the months in letters to months in numbers.
Thanks for your answers
Jules PASCO
Jules PASCO 2021년 6월 1일
When I try to go from 04-May-2021 11:00:00 to 04-05-2021 11:00:00, matlab returns this:
@Jules PASCO: Of course it will not work: does the "InputFormat" match the input data format? (hint: no)
This is what you are telling MATLAB (and us in your question) that your dates looks like:
'yyyy-MM-dd e HH:mm:ss e'
whereas this is (apparently) what your dates actually look like:
'04-May-2021 11:00:00'
Are they the same format? (hint: no)
"The only problem is that I can't turn this format into a datetime, it stays a string."
It works for me:
S = '04-May-2021 11:00:00';
T = datetime(S, 'InputFormat','dd-MMM-yyyy HH:mm:ss') % !!! DATETIME !!!
T = datetime
04-May-2021 11:00:00
T.Format = 'yyyy-MM-dd HH:mm:ss' % !!! Still DATETIME !!!
T = datetime
2021-05-04 11:00:00
Did you specify the InputFormat to actually match the data you have?
Jules PASCO
Jules PASCO 2021년 6월 2일
Ok! It works now!
To make the dates match the values, I imported my data files as arrays instead of vectors/matrices like before, but I have the impression that matlab differentiates arrays from matrices, which prevents me from doing the date conversion...
If I can't do it any other way I'd switch my array to matrix but the point of array is that it recognizes dates.

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

Allen
Allen 2021년 5월 26일

0 개 추천

Try running a string replacement to remove "T" and "Z" from your string before reformatting to datetime.
str = "2021-05-11T14:11:00Z";
datetime(regexprep(str,["T","Z"],[" ",""]))
ans = datetime
11-May-2021 14:11:00

댓글 수: 1

The "T" is specified in ISO 8601, it would be a very unfortunate if DATETIME could not handle it:
The "Z" refers to the Zulu time zone:
and as such it conveys important information which cannot be disregarded:
S = '2021-05-11T14:11:00Z';
T = datetime(S,'InputFormat','yyyy-MM-dd''T''HH:mm:ssZ', 'TimeZone','UTC')
T = datetime
11-May-2021 14:11:00
T.TimeZone = 'Asia/Shanghai'
T = datetime
11-May-2021 22:11:00

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

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

제품

질문:

2021년 5월 26일

댓글:

2021년 6월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by