Adding number of seconds from changing base date

9 次查看(过去 30 天)
This ones been giving me some trouble. I'm calling a file where the 'time' variable is the seconds since midnight of the day of interest which constantly changes:
In this test example, I am attempting to call four days of data at one time in a for loop. 's' is just the changing file name, but here when I attempt to take the 'time_raw' from the time variable for each file, it gives me the seconds since midnight of the current day, not the days of interest. I attempted to make some datetime values (t) of the days of interest and thought to concatenate them together (day of interest plus datetime of seconds since midnight) but I am unable to at this time. Feel like I'm missing one line of code to fix this, just not sure what that would be.
Currently, my 't2' variable is the correct number of seconds found in 'time' but for August 12, 2020 not April 16-19, 2010.

采纳的回答

Steven Lord
Steven Lord 2020-8-13
You can construct a datetime directly from the number of seconds. From the documentation page for datetime:
"t = datetime(X,'ConvertFrom',dateType) converts the numeric values in X to a datetime array t. The dateType argument specifies the type of values in X."
One of the dateType options is 'epochtime', "Number of seconds since an epoch." If you use this "You must also specify epochValue, which is a scalar datetime, or a character vector or string scalar representing the epoch time."
>> datetime(100, 'ConvertFrom', 'epochtime', 'Epoch', datetime(2010, 4, 16))
ans =
datetime
16-Apr-2010 00:01:40
  2 个评论
Eric Beamesderfer
Eric Beamesderfer 2020-8-13
Thanks, Steven. This and the other answer are very close to working for me. However, I should have mentioned the number of seconds resets with each file/day. So on the next day, its back to close to zero. Currently, this solution restarts the number of seconds but doesn't update the correct day.
Eric Beamesderfer
Eric Beamesderfer 2020-8-13
Slightly modifying your answer to add a changing date worked. Thanks again!

请先登录,再进行评论。

更多回答(1 个)

Mohammad Sami
Mohammad Sami 2020-8-13
Since the time is given to you as seconds since some date. you can read it as a double and then add it to the date of interests.
dateofinterest = datetime(2010,04,16);
% read the seconds
s = 1:100; % replace with your code to read the value.
out = dateofinterest + seconds(s);
  1 个评论
Eric Beamesderfer
Eric Beamesderfer 2020-8-13
Thank you, Mohammad. This didn't work at first, as the time (number of seconds since midnight) restarts with each day/file. Initially, your solution created a time array where after midnight, the time restarted, but it was still for the first day (April 16th) not the second day (April 17th). I slightly modified your answer and then it worked.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by