# how to convert the date and time into separate column

3 views (last 30 days)
muhammad choudhry on 28 Dec 2021
Edited: Siddharth Bhutiya on 30 Dec 2021
Hi,
I have dateandtime in the 7th column of the big data Excel file, which is from 1 to 179 rows. I want to separate the date and time into the next 2 columns. I am trying to do that but lack the experience. I have figured it out how to do it in Excel but I have like 100 spreadsheets and it's not ideal to do it there. Below is the code I have tried.
c=b(:,7);
integer(c)-(c) % basically date is the integer and I was taking away the date from the time to achieve the timestamp.
##### 2 CommentsShowHide 1 older comment
Image Analyst on 28 Dec 2021
179 rows is hardly "big". And 100 files is not a lot. Please attach one, like 'head.TXT' so we can get to work on solving your problem. We need to know the format of your date and time string. For example is it like
datestr(now)
ans = '28-Dec-2021 22:51:32'
See the FAQ for how to process a sequence of files:

Siddharth Bhutiya on 30 Dec 2021
Edited: Siddharth Bhutiya on 30 Dec 2021
Just based on the information in your description, I am assuming your dateandtime is a datetime variable in your table and you want to get the whole date and time component and store these as separate variables in your table. You could spearate the date and time using one of the following two methods.
If you are using 21b, you could use the timeofday function with two outputs to get the whole day and time values from a dateime
>> DT = datetime(2021,12,1:5,1:5,11:15,0)'
DT =
5×1 datetime array
01-Dec-2021 01:11:00
02-Dec-2021 02:12:00
03-Dec-2021 03:13:00
04-Dec-2021 04:14:00
05-Dec-2021 05:15:00
>> [T,D] = timeofday(DT)
T =
5×1 duration array
01:11:00
02:12:00
03:13:00
04:14:00
05:15:00
D =
5×1 datetime array
01-Dec-2021
02-Dec-2021
03-Dec-2021
04-Dec-2021
05-Dec-2021
Before 21b you could do it using timeofday and dateshift as follows:
>> T = timeofday(DT)
T =
5×1 duration array
01:11:00
02:12:00
03:13:00
04:14:00
05:15:00
>> D = dateshift(DT,'start','day')
D =
5×1 datetime array
01-Dec-2021
02-Dec-2021
03-Dec-2021
04-Dec-2021
05-Dec-2021
One thing I would like to point out here is that the dates D are datetimes, so they would still have a time component, which would be midnight of that day.