datetime loosing midnight timestamps

Marc Hanssens on 26 Jul 2023 (Edited on 26 Jul 2023)
Latest activity Reply by Marc Hanssens on 31 Jul 2023

I am getting a NaT from the datetime function because it doesn't interpretp the date as a date/time?
I currently use this data (time) as this:
{'2023-07-26T23:00:00Z'}
{'2023-07-26T23:30:00Z'}
{'2023-07-27' }
{'2023-07-27T00:30:00Z'}
{'2023-07-27T01:00:00Z'}
and I get tStamps as this:
27-Jul-2023 09:00:00
27-Jul-2023 09:30:00
NaT %how do I not get this NaT? but 27-Jul-2023 10:00:00 which is 27-Jul-2023 00:00:00 gmt/utc
27-Jul-2023 10:30:00
27-Jul-2023 11:00:00
using this code for the datetime function
infmt ='yyyy-MM-dd''T''HH:mm:00Z';
tStamps = datetime(time,"InputFormat",infmt,'TimeZone','Australia/Brisbane');
Christopher Stapels
Christopher Stapels on 28 Jul 2023
Is the datetime cell array you show returned from ThingSpeak?
What happends if you seperate the format and time zone operations?
Worst case plan might be to scan for the value 9:30 and then just fix the next one in your code.
Marc Hanssens
Marc Hanssens on 30 Jul 2023 (Edited on 31 Jul 2023)
Hi Chris, this data is not from thingspeak:
{'2023-07-26T23:00:00Z'}
{'2023-07-26T23:30:00Z'}
{'2023-07-27' }
{'2023-07-27T00:30:00Z'}
{'2023-07-27T01:00:00Z'}
A thought I had was to check the string length and if its small, then append it with "T00:00:00Z" but I'm not sure if I need to create a loop and check each entry individually or if there is a matlb function that I can use on the whole data set like the datetime function?
When I didn't include the timezone in the datetime operation, I got a message saying that I should include it as it was expecting it.
Marc Hanssens
Marc Hanssens on 31 Jul 2023
I got it working using this code:
for i=1:length(time)
x = length(time{i});
if x < 12
time{i}= [time{i},'T00:00:00Z']; % fill in time when its missing from the date
%display(time(i));
end
end
infmt ='yyyy-MM-dd''T''HH:mm:00Z';
tStamps = datetime(time,"InputFormat",infmt,'TimeZone','Australia/Brisbane');
Christopher Stapels
Christopher Stapels on 31 Jul 2023
From my quick experiments, datetime does not seem to be flexible enough to attack multiple input formats at once (thats a lot to ask). I think a loop is your best bet.
If you want to use the datetime command in the loop, you could use try catch and then just use datetime without the inputformat when it fails.
Marc Hanssens
Marc Hanssens on 26 Jul 2023 (Edited on 26 Jul 2023)
it looks like fillmissing might be able to be used to fill the missing time with T00:00:00Z but I can't find a good example of this?