datetime format with T time demarcation

I have data that I've read in with datetime format that includes a T such as:
>> ais_data.BaseDateTime(3)
ans =
datetime
2023-08-01T11:07:56
If I change the value, how do I format the answer. For example, I would like to increment the minute, like such 2023-08-01T11:08:56
However, this results in the following error
>> datetime(2023-08-01T11:08:56)
datetime(2023-08-01T11:08:56)
Error: Unexpected 'T11'. Check for missing multiplication operator.
How do make a datetime number like the one I'm seeing in the example at the top?

1 个评论

dt1 = datetime(2023,8,1,11,07,56, "Format","yyyy-MM-dd'T'HH:mm:ss")
dt1 = datetime
2023-08-01T11:07:56
dt2 = dt1 + minutes(1)
dt2 = datetime
2023-08-01T11:08:56

请先登录,再进行评论。

 采纳的回答

You're trying to convert a string representation so it has to be a string variable of some type... (+)
datetime('2023-08-01T11:08:56')
ans = datetime
01-Aug-2023 11:08:56
If you have a datetime variable already, then increment it instead of trying to convert..
ais_data.BaseDateTime=datetime('2023-08-01T11:07:56')
ais_data = struct with fields:
BaseDateTime: 01-Aug-2023 11:07:56
newDateTime=ais_data.BaseDateTime+minutes(1)
newDateTime = datetime
01-Aug-2023 11:08:56
(+) If you have a value as datetime, to convert the string representation would be
ais_data.BaseDateTime.Format='yyyy-MM-dd''T''HH:mm:ss'; % default format doesn't include T
date_str=string(ais_data.BaseDateTime)
date_str = "2023-08-01T11:07:56"
datetime(date_str)
ans = datetime
01-Aug-2023 11:07:56

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Data Type Conversion 的更多信息

产品

版本

R2025a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by