Adding number of seconds from changing base date
5 次查看(过去 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.
0 个评论
采纳的回答
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
更多回答(1 个)
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!