Convert a time string to double

조회 수: 12 (최근 30일)
Tasnim Sayadi
Tasnim Sayadi 2022년 8월 4일
댓글: Star Strider 2022년 8월 5일
Hi everyone,
I have a timestamp vector in seconds (in range 1.6573e+9 ) and converted it to datetime (I get for expl. 08-Jul-2022 09:42:56)
t = datetime(1970,1,1) + seconds(timeStamps) + hours(2);
I'm not sure why I need to add 2 hours, but it works correctly. Now I want to search to see if the date I entered is included in timeStamps. Since my input date is a string and timeStamps is a double, I need to convert my date to double
ts = datenum( t - datetime(1970,1,1) - hours(2) ) ;
I used the same converted vector t to test if I get the same values for timeStamps, but I get a wrong answer (in range 1.9181e+04)
So I wonder if someone can help me to solve this problem? Thank you
  댓글 수: 2
Stephen23
Stephen23 2022년 8월 4일
Don't use deprecated DATENUM, DATEVEC, or DATESTR.
Instead you should use the methods and functions that support DATETIME objects:
Steven Lord
Steven Lord 2022년 8월 4일
I have a timestamp vector in seconds (in range 1.6573e+9 ) and converted it to datetime
Instead of manually creating the datetime representing January 1st, 1970 I'd suggest using the 'ConvertFrom' option for datetime with the 'posixtime' option.
...
Since my input date is a string and timeStamps is a double, I need to convert my date to double
No. Convert your string representing a date and time to a datetime array instead. Then use isbetween or relational operators (greater than, less than, etc.) on the datetime arrays.

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2022년 8월 4일
I am not certain what you are doing, however the isbetween function (that does not require conversion out of datetime) may be what you want.
  댓글 수: 2
Tasnim Sayadi
Tasnim Sayadi 2022년 8월 5일
After searching for the time I would like to read for example the Temperature, Humidity,... . Therefore I want to know not only if the date and time exists but also the index. isbetween returns only a logical value (true/false). Is there a way to get the index too?
Star Strider
Star Strider 2022년 8월 5일
The isbetween funciton returns a logical vector. Assuming your data are column-oriented, you would get all the rows with something like this —
Lv = isbetween(t, tupper, tupper); % Logical Vector
AllData = T{Lv,:}; % Return Matrix Of Relevant Data
where ‘t’ are the datetime data and ‘T’ is the table that contains it and the other data. (Note the use of curly brackets {} to return a matrix rather than a table.)
See the documentation I linked to earlier for more information.
.

댓글을 달려면 로그인하십시오.

카테고리

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