How to read this date format yyyymmddHHMMSS.SS ?

118 visualizaciones (últimos 30 días)
hanif hamden
hanif hamden el 14 de En. de 2020
Editada: Adam Danz el 15 de En. de 2020
Hi everyone,
Since my datetime data in the form of yyyymmddHHMMSS.SS (20020804122604.66). How I would like to read this type of format and convert it to datenum? How can I do this in matlab?
dates={'20020804122604.66';'20020804122610.66'}
datenum(dates,'yyyymmddHHMMSS.SS')
I can only managed to convert it if this format without miliseconds (yyyymmddHHMMSS).
dates={'20020804122604';'20020804122610'}
datenum(dates,'yyyymmddHHMMSS')
Best

Respuesta aceptada

Adam Danz
Adam Danz el 14 de En. de 2020
Editada: Adam Danz el 15 de En. de 2020
Datetime values are much better than datenum values. Nevertheless, here's how to convert your datestrings to both.
% Date strings of format "yyyymmddHHMMSS.SS"
dates={'20020804122604.66';'20020804122610.66'}
% Convert to datetime
dtm = datetime(dates,'InputFormat','yyyyMMddHHmmss.SS');
% Convert to datenum (if you must; not recommended)
dnm = datenum(dtm)
With the datetime values, you can also specify the output format,
dtm = datetime(dates,'InputFormat','yyyyMMddHHmmss.SS','Format','yyyyMMddHHmmss.SS');
Why are datetime arrays better than datenum?
Datetime arrays are much more useful than datenum values and are the primary way to store and represent date and time data in Matlab. There are many core functions that support datetime arrays that become difficult to perform with datenum values. Arithmetic and datetime comparisons are much easier with datetime values. You can specify time zones and specify the human-readable format of datetime values (including POSIX time). Datetime values are especially useful when dates and times are plotted since they appear as date/times in the axis ticks.
See this list of functions used to create, split, manipulate, and convert datetime values.
Nearly none of the capabilities mentioned above support datenum values.
  2 comentarios
hanif hamden
hanif hamden el 15 de En. de 2020
Thank you so much. But may I know why it is not recommended using datenum?
Adam Danz
Adam Danz el 15 de En. de 2020
I've added a section at the end of my answer to explain ;)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Productos


Versión

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by