Adding time to time string

18 ビュー (過去 30 日間)
Arcot Manjunath Shreepal
Arcot Manjunath Shreepal 2022 年 7 月 5 日
回答済み: Eric Sofen 2022 年 7 月 5 日
I am dealing with two strings, one for date and the other for time. The date string is in the format 'yyyy/MM/dd' and time is in the format 'HH:mm:ss'. I need to perform two tasks here:
  1. I need to merge both the strings to the format 'yyyy-MM-dd HH:mm:ss'.
  2. I have a 60x1 list that contains numbers(which are seconds). To the merged output I need to create a loop to add the seconds and create a new list with all the 60 updated times.
I hope I made sense. Kindly help me with this problem and let me know if you need any further clarification.

採用された回答

Garmit Pant
Garmit Pant 2022 年 7 月 5 日
Hello Arcot
You can use the following code snippet as an example and adapt it to your own needs:
DateString = '2014/05/26'; %Date
timeString = '21:24:05'; %Time
% Convert date to a recognisable format
newDateString = strrep(DateString,'/','-');
%Join the date and time
joinInp = [newDateString ' ' timeString];
% Create the datetime object in the required format
t = datetime(joinInp,'InputFormat','yyyy-MM-dd HH:mm:ss')
t = datetime
26-May-2014 21:24:05
%Add 5 seconds to the created datetime object
tnew = t + seconds(5)
tnew = datetime
26-May-2014 21:24:10
  1 件のコメント
Arcot Manjunath Shreepal
Arcot Manjunath Shreepal 2022 年 7 月 5 日
Thank you Garmit.
The output is as desired. How do I format the seconds to hold fractions upto 3 decimals. example: 21:24:10.345

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

その他の回答 (2 件)

Karim
Karim 2022 年 7 月 5 日
Note that it would be much easier to answer if you would have added exmaple data, anyhow you can use the following approach:
% random dates
DateStrings = ["2022-02-10","2021-04-20","2020-06-30"];
dates = datetime(DateStrings,'Format','yyyy-MM-dd')';
% random times
TimeStrings = ["04:21:30","07:19:21","13:51:33"];
times = datetime(TimeStrings,'Format','HH:mm:SS')';
% format both columns to yyyy-MM-dd HH:mm:SS for proper merging
dates = datetime(dates,'Format','yyyy-MM-dd HH:mm:SS');
times = datetime(times,'Format','yyyy-MM-dd HH:mm:SS');
% merge the dates and times
FullDataTime = dates+timeofday(times)
FullDataTime = 3×1 datetime array
2022-02-10 04:21:30 2021-04-20 07:19:21 2020-06-30 13:51:33

Eric Sofen
Eric Sofen 2022 年 7 月 5 日
Here's another slightly different approach. There's no need to replace "/" with "-" for datetime to parse it. Duration can parse "timer" formats, then you can just add together the datetime and duration without needing to use timeofday.
DateStrings = ["2022/02/10","2021/04/20","2020/06/30"];
TimeStrings = ["04:21:30","07:19:21","13:51:33"];
d = datetime(DateStrings,'InputFormat','yyyy/MM/dd','Format', 'yyyy-MM-dd HH:mm:ss') + duration(TimeStrings,"InputFormat","hh:mm:ss")
d = 1×3 datetime array
2022-02-10 04:21:30 2021-04-20 07:19:21 2020-06-30 13:51:33

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by