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 Comments

jonas
jonas on 4 Sep 2018
Edited: jonas on 4 Sep 2018
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?
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.
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.
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.
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']
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.
I wrote that before I saw the answer that you wrote below. I am working on it now.

Sign in to comment.

 Accepted Answer

jonas
jonas on 4 Sep 2018
Edited: jonas on 4 Sep 2018
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 Comments

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...!
Its been a while since I used datevec but the output format is [year, month, day] no?
If you are right then I used wrong order. I will try it again
nothing, the same problem (negative seconds)
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.
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]
Thank you Jonas for your time and help!
My pleasure! Don't forget to accept if the answer was helpful!

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 4 Sep 2018

Commented:

on 5 Sep 2018

Community Treasure Hunt

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

Start Hunting!