how to convert the date and time into separate column

조회 수: 5(최근 30일)
muhammad choudhry
muhammad choudhry 2021년 12월 28일
편집: Siddharth Bhutiya 2021년 12월 30일
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.
b=readtable('head.TXT');
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
Image Analyst
Image Analyst 2021년 12월 28일
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
Siddharth Bhutiya 2021년 12월 30일
편집: Siddharth Bhutiya 2021년 12월 30일
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.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by