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
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
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)
See the FAQ for how to process a sequence of files:
采纳的回答
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
>> 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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!