How can I change timestamp format?

2 visualizaciones (últimos 30 días)
Aakash Soni
Aakash Soni el 25 de En. de 2023
Comentada: Aakash Soni el 25 de En. de 2023
Hello Matlab Community,
I have a dataset with timestamp which require some modification.
I would like to change the format of fractional seconds. For example, if a timestamp is 14:41:57:1, it should be changed to 14:41:57.001.
I tried following code but not getting success. I am getting 14:41:57.100 instead of 14:41:57.001.
>> a
a =
2×1 cell array
{'09.02.2022 14:41:56:999'}
{'09.02.2022 14:41:57:1' }
>> t = datetime(a,'InputFormat','dd.MM.yyyy HH:mm:ss:SSS')
t =
2×1 datetime array
09-Feb-2022 14:41:56
09-Feb-2022 14:41:57
>> t.Format = 'dd.MM.yyyy HH:mm:ss.SSS'
t =
2×1 datetime array
09.02.2022 14:41:56.999
09.02.2022 14:41:57.100
>>
How can I change timestamp format in this case?
Thank you,
Aakash.

Respuesta aceptada

Stephen23
Stephen23 el 25 de En. de 2023
Editada: Stephen23 el 25 de En. de 2023
The best solution is to fix the source. Otherwise:
C = {'09.02.2022 14:41:56:999';'09.02.2022 14:41:57:1'}
C = 2×1 cell array
{'09.02.2022 14:41:56:999'} {'09.02.2022 14:41:57:1' }
D = regexprep(C,{':(\d\d)$',':(\d)$'},{':0$1',':00$1'});
T = datetime(D,'InputFormat','dd.MM.yyyy HH:mm:ss:SSS');
T.Format = 'dd.MM.yyyy HH:mm:ss.SSS'
T = 2×1 datetime array
09.02.2022 14:41:56.999 09.02.2022 14:41:57.001
  1 comentario
Aakash Soni
Aakash Soni el 25 de En. de 2023
Thanks for the solution. It worked like a charm.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by