How can I change timestamp format?

7 ビュー (過去 30 日間)
Aakash Soni
Aakash Soni 2023 年 1 月 25 日
コメント済み: Aakash Soni 2023 年 1 月 25 日
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
  1 件のコメント
Aakash Soni
Aakash Soni 2023 年 1 月 25 日
Thanks for the solution. It worked like a charm.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by