how to convert the date and time into separate column

23 次查看(过去 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 个评论
Voss
Voss 2021-12-28
I imagine that you want integer(c) to return the greatest integer less than or equal to the argument c, but - at least in my version of MATLAB - no function integer() exists. You can probably use floor() to do what you want, but you're going to want to subtract out the integer part to leave the time-of-day part:
c-floor(c)
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 个)

类别

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