How can I change timestamp format?

2 visualizzazioni (ultimi 30 giorni)
Aakash Soni
Aakash Soni il 25 Gen 2023
Commentato: Aakash Soni il 25 Gen 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.

Risposta accettata

Stephen23
Stephen23 il 25 Gen 2023
Modificato: Stephen23 il 25 Gen 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

Più risposte (0)

Categorie

Scopri di più su Numeric Types in Help Center e File Exchange

Tag

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by