Datetime 'convertfrom' forcing dates to be Jan2019

조회 수: 20 (최근 30일)
Oliver Higbee
Oliver Higbee 2019년 10월 1일
댓글: Oliver Higbee 2019년 10월 2일
Hi there,
I'm trying to convert my data's date format from MM/dd/yyyy to dd/MM/yyyy.
I'm using;
load conductortemps17sept19_4 %datafile
T = conductortemps17sept194;
T.Properties.VariableNames = {'Datapoint' 'LogDate' 'LogTime' 'Broken' 'Uncoated' 'Solvent' 'Water' 'MV01' 'MV06'};
T(1,:) = []; %Remove headings from data
T.LogDate = datetime(T.LogDate, 'ConvertFrom', 'MM/dd/yyyy', 'Format','dd/MM/yyyy'); %convert to british dates
T.LogDate.Format = 'HH:mm:ss dd/MM/yyyy';
T.LogTime.Format = 'HH:mm:ss dd/MM/yyyy';
T.Time = T.LogDate + timeofday(T.LogTime); %combine date and time
The 'Logdate' data ranges 17th September to 1st October (today). However it's converting all dates to be Jan2019.
This code has been working fine for the past few weeks, giving correct dates. What am I doing wrong?
The attachments are the loaded file and the output I'm seeing for dates.
Kind regards,
Oliver
  댓글 수: 2
Stephen23
Stephen23 2019년 10월 1일
Your uploaded table does not contain the variable/column LogDate, so we have no way to know how your code relates to your uploaded data, if at all (it would throw an error).
Oliver Higbee
Oliver Higbee 2019년 10월 2일
Apologies Stephen, I omitted the variable naming line from the orginal post. I'll add it in now

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2019년 10월 1일
편집: Stephen23 2019년 10월 1일
Your code and question do not seem to be particularly related to your uploaded data, or show some signs of confusion. For example, your table variables do not include LogDate, in fact the variables are named Enabled, True, True1, etc.:
Note also that the second column/variable is already a datetime array, so there is no point in converting it to datetime array when it already is one. But when we actually look at that data, we notice a major problem:
>> T = conductortemps17sept194;
>> T(1,:) = []; % Should be done when importing the data
>> T.True.Format
ans =
mm/dd/yyyy
Whoever imported that data or created that datetime array did not read the documentation for the format syntax, and seems to have mistakenly imported that date as Minute/Day/Year! As the datetime documentation clearly states, mm is for minutes, MM is for months.
So of course when we change the datetime format to show the complete date + time
>> T.True.Format = 'yyyy-MM-dd HH:mm:ss'; % following the DATETIME documentation
then the month uses its default value (1 = January) and the minutes have the values that you probably think are the months:
So far everything is working as documented. Now we can easily add those times-of-the-day:
>> T.True = T.True + timeofday(T.True1);
Giving exactly the times and dates that we expect (given that someone imported months as minutes):
Note that the header lines and datetime formats should be correctly specified when importing the data (it is better to fix the problem at its source than try to patch it later, which I am not going to attempt to do).
  댓글 수: 1
Oliver Higbee
Oliver Higbee 2019년 10월 2일
Thanks Stephen!
Apologies for confusing matters by not including the varaible names line in the code posted. Great point RE not needing to convert array type, it hadn't occured to me before.
I had actually looked at the format prior to posting here but still didn't spot the minutes instead of months error. So thankyou for spotting the error and the great explanation, it has been immesively helpful.
Kind regards,
Oliver

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by