Convert date string to Date number error

Good morning,
Little stuck on this. Using Matlab 2015b and looking at converting this date string into a datenumber using datenum.
The date string im trying to convert is:
2015-05-31 14:52:27.420
What im using:
op_datetime = '2015-05-31 14:52:27.420'
format = 'yyyy-mm-dd HH:MM:SS.Z'
datenum(op_datetime, format)
This gives me the following error:
Error using datenum (line178)
DATENUM failed.
Caused by:
Error using dtstr2dtnummx
Failed on converting date string to date number.
The internal date time format that Matlab gives is:
I have tried changing the split seconds into a different format ie.
format = yyyy-mm-dd HH:MM:SS.SSS
But this gives me an error saying:
Error using datenum(linie178)
DATENUM failed.
Caused by:
Error using matlab.internal.datetime.cnv2icudf (line 137)
Unrecognized second format. Format string: yyyy-mm-dd HH:MM:SS.SSS
So have tried other formats and different ways but cant get anything to work. Is this due to the split seconds format?
Any help is appreciated.
Thank you.

답변 (1개)

Stephen23
Stephen23 2016년 2월 18일
편집: Stephen23 2016년 2월 19일

0 개 추천

When you read the datenum documentation you will find that the correct millisecond token is FFF:
yyyy-mm-dd HH:MM:SS.FFF
for example:
>> op_datetime = '2015-05-31 14:52:27.420';
>> datenum(op_datetime,'yyyy-mm-dd HH:MM:SS.FFF')
ans =
7.3612e+05

댓글 수: 3

Thanks Stephen.
I have tried this but i still get the same error message..
Error using datenum (line178)
DATENUM failed.
Caused by:
Error using dtstr2dtnummx
Failed on converting date string to date number.
Is there something else I am missing?
Peter Perkins
Peter Perkins 2016년 2월 18일
편집: Stephen23 2016년 2월 18일
Does this not work for you?
>> datenum('2015-05-31 14:52:27.420','yyyy-mm-dd HH:MM:SS.FFF')
ans =
7.3612e+05
If you're in R2015b, you should consider using datetime, and moving away from datenum and datestr.
@Dan Howard: It is possible that there might be some characters in the string that are causing this. Control characters and other unprintable characters sometimes get read from files or other data sources, are very difficult to spot.
You should test the code that I gave (and that Peter Perkins used) with the string from the question, not using any other data source. You should get the same result as we did.
Secondly, please tell us exactly what is output from this command:
double(op_datetime)

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

카테고리

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

질문:

2016년 2월 18일

편집:

2016년 2월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by