Datetime 'convertfrom' forcing dates to be Jan2019
13 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 1 Oct 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).
Accepted Answer
Stephen23
on 1 Oct 2019
Edited: Stephen23
on 1 Oct 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).
More Answers (0)
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!