How can I change timestamp format?

2 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2023-1-25
编辑:Stephen23 2023-1-25
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by