Datetime 'convertfrom' forcing dates to be Jan2019

20 visualizaciones (últimos 30 días)
Oliver Higbee
Oliver Higbee el 1 de Oct. de 2019
Comentada: Oliver Higbee el 2 de Oct. de 2019
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 comentarios
Stephen23
Stephen23 el 1 de Oct. de 2019
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 el 2 de Oct. de 2019
Apologies Stephen, I omitted the variable naming line from the orginal post. I'll add it in now

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 1 de Oct. de 2019
Editada: Stephen23 el 1 de Oct. de 2019
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 comentario
Oliver Higbee
Oliver Higbee el 2 de Oct. de 2019
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by