Hello all,
I would like to conver a time to num format so i can do some basic math on it and then I wold like to format t back to day time format.
TimeFormat = 'yyyymmddHHMMSS';
st_file=cell(1,1);%Memory prealocation
t = w_files{2,1}(1,"time") %I am pulling a time from my data: 2022-08-08 10:18:12.000
t =
table
time
_______________________
2022-08-08 10:18:12.000
t = table2array(t)%Converting it to cell because datestr does not work as it is right now
t =
datetime
2022-08-08 10:18:12.000
t = datestr(t,TimeFormat);%Making it a str
t =
'20220808101812'
t = datetime(t,"InputFormat",TimeFormat)%Changing it back to date time format
t =
datetime
08-Oct-2020 08:22:00
Is this how is the sotware intended to work? Where am I making a mistake and how can I avoid it in the future. Thanks!

 採用された回答

Karim
Karim 2022 年 8 月 29 日

0 投票

From the looks of it, you need to change the months (M) and minutes (m) symbols in your time format to convert the string into expected datetime. See below:
TimeFormat = 'yyyyMMddHHmmSS';
t = "20220808101812"
t = "20220808101812"
t = datetime(t,"InputFormat",TimeFormat)
t = datetime
08-Aug-2022 10:18:00

2 件のコメント

Karel Starý
Karel Starý 2022 年 8 月 29 日
編集済み: Karel Starý 2022 年 8 月 29 日
This works. After s gooli found out that date format is different for "datestr" function and "datetime" func. Wat a beatifull way to write a software, right :D
Thans anyway!
Walter Roberson
Walter Roberson 2022 年 8 月 29 日
datetime() was written using ISO standard codes for date and time. datestr() is older than the ISO standard.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2022 年 8 月 29 日

0 投票

I would like to conver a time to num format so i can do some basic math on it
What math do you want to do to the time data? There may be a way to avoid converting from time to number and back. For example:
T = datetime('today')
T = datetime
29-Aug-2022
nextMonth = T + calmonths(1)
nextMonth = datetime
29-Sep-2022
startOfThisMonth = dateshift(T, 'start', 'month')
startOfThisMonth = datetime
01-Aug-2022

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

製品

リリース

R2021b

タグ

質問済み:

2022 年 8 月 29 日

回答済み:

2022 年 8 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by