convert date and time in seconds

Hello everyone, I have a table with several variables. Among others, I have 2 columns with date and time. These 2 columns are cells with strings ('iscellstr'=1 and 'ischar'=0). How can I convert them in seconds? thank you very much!

7 件のコメント

jonas
jonas 2018 年 9 月 4 日
編集済み: jonas 2018 年 9 月 4 日
Question unclear. You cannot convert date and time (datetime) to seconds (duration). For example, how do you convert today's date to seconds?
What is your desired output? Can you upload a sample file?
Walter Roberson
Walter Roberson 2018 年 9 月 4 日
You could calculate number of seconds since some event, such as by converting serial date numbers from days into seconds. This would, however, be prone to errors due to leap seconds and calendar changes that happened at different dates in different parts of the world.
Nivodi
Nivodi 2018 年 9 月 4 日
The date and the time are referred to measurements that had done at the specific date and time. I have to convert them in seconds in order to know how many seconds were spent from the beginning of this year (for instance for the measurement of 9/03/2017, 15:45:03, how many seconds are from 01/01/2017, 00:00:00). The point is that every "seconds" that will be calculated has to be unique with no repetitions. That's why I thought of this way. Because form the beginning of the year every second that passes and is added to the previous one gives a unique result. In the beginning, I converted the time to seconds(with datevec) and then I added the serial numbers of the date which I took them from datenum, but a lot of values were duplicated.
jonas
jonas 2018 年 9 月 4 日
But you will also get unique values if you convert the time stamps (date+time) to a datetime array. Unless there is a specific reason you need the data to be in seconds (or any other duration), then I'd strongly recommend simply converting your time-vector to a datetime array and work with that.
Nivodi
Nivodi 2018 年 9 月 4 日
編集済み: Walter Roberson 2018 年 9 月 4 日
I did this:
date_time=[year, month,day,hour,min,sec, 'dd.mm.yyyy dd.mm.yyyy']
in order to take one column (the year, month etc, I exctracted them with datevec), and I took this:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in UnsuccefullScans (line 30)
date_time=[year, month,day,hour,min,sec, 'dd.mm.yyyy HH:MM:SS']
jonas
jonas 2018 年 9 月 4 日
Not sure what you are doing, but that is not the correct syntax. I wrote some lines of code in the answer section to give you an idea of what I meant.
Nivodi
Nivodi 2018 年 9 月 4 日
I wrote that before I saw the answer that you wrote below. I am working on it now.

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

 採用された回答

jonas
jonas 2018 年 9 月 4 日
編集済み: jonas 2018 年 9 月 4 日

1 投票

Here's a solution with datetime
data=load('matlab.mat')
T=data.T1_B6_09032017;
TimeOfDay=duration(T.time)
Date=datetime(T.date,'inputformat','dd.MM.yyyy')
t=Date+TimeOfDay;
If you really want the output in seconds, here is how you calculate the duration from the first measurement:
ts=seconds(t-t(1));
I suggest you then put the data in a timetable
TT=timetable(t,T);
TT=splitvars(TT);

8 件のコメント

Nivodi
Nivodi 2018 年 9 月 4 日
編集済み: Walter Roberson 2018 年 9 月 4 日
Jonas, when I used your code I took this message: "Input data must be a numeric matrix with three columns, or three separate numeric arrays.
Error in UnsuccefullScans (line 20)
TimeOfDay=duration(T.time)'. So I changed a little bit the code as follows:
time=table2array(T1_B6_09032017(:,15));
[~,~,~,hour, min, sec]=datevec(time,'HH:MM:SS');
time=[hour,sec,min]
TimeOfDay=duration(time)
date=table2array(T1_B6_09032017(:,14));
[day,month,year,~,~,~]=datevec(date,'dd.mm.yyyy');
date=[day,month,year]
Date=datetime(date,'inputformat','dd.mm.yyyy')
t=Date+TimeOfDay;
seconds=seconds(t-t(1));
The problem is that some of the seconds are negative values...!
jonas
jonas 2018 年 9 月 4 日
Its been a while since I used datevec but the output format is [year, month, day] no?
Nivodi
Nivodi 2018 年 9 月 4 日
If you are right then I used wrong order. I will try it again
Nivodi
Nivodi 2018 年 9 月 4 日
nothing, the same problem (negative seconds)
jonas
jonas 2018 年 9 月 4 日
The reason the original code is not working is probably because you are using an older release (2018a here), and there has been some recent changes to datetime and duration formats. I will fix the code so that it works for older releases. In the meantime you should try the other solution.
jonas
jonas 2018 年 9 月 4 日
編集済み: jonas 2018 年 9 月 4 日
This should work in 2015b
data=load('matlab.mat');
T=data.T1_B6_09032017;
[~,~,~,h,m,s]=datevec(T.time);
TimeOfDay=duration(h,m,s);
Date=datetime(T.date,'inputformat','dd.MM.yyyy');
t=Date+TimeOfDay;
ts=seconds(t-t(1));
min(ts)
ans =
0
As you can see, no negative seconds. The problem with your code is here:
time=[hour,sec,min]
should be [hour,min,sec]
Nivodi
Nivodi 2018 年 9 月 5 日
Thank you Jonas for your time and help!
jonas
jonas 2018 年 9 月 5 日
My pleasure! Don't forget to accept if the answer was helpful!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

タグ

質問済み:

2018 年 9 月 4 日

コメント済み:

2018 年 9 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by