Date time conversion fail
1 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Karim
2022-8-29
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 = datetime(t,"InputFormat",TimeFormat)
2 个评论
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
2022-8-29
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')
nextMonth = T + calmonths(1)
startOfThisMonth = dateshift(T, 'start', 'month')
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!