How can I change timestamp format?

2 views (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 25 Jan 2023
Edited: Stephen23 on 25 Jan 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

More Answers (0)

Categories

Find more on Numeric Types in Help Center and File Exchange

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!