Convert date string to Date number error
15 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
Stephen23
2016 年 2 月 18 日
編集済み: Stephen23
2016 年 2 月 19 日
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 件のコメント
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.
Stephen23
2016 年 2 月 18 日
@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)
参考
カテゴリ
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!