Convert date data in datetime
25 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dear Matlab Community,
I have uplaoded the small subset of the data which I am using;
I am converting it in dateime using:
date = datetime(string(data.Date),'InputFormat','yyyyMMddHH' ) ;
I have also attached the output file.
I will also like to add that the original file is in csv format, I am using 'readtable' to read and process it in Matlab.
The problem is date corresponding to 24hrs is not coverted in datetime.
Any help will be highly appreciated.
Many thanks.
5 commentaires
Stephen23
le 19 Déc 2023
"I will also like to add that the original file is in csv format, I am using 'readtable' to read and process it in Matlab."
Please upload a sample CSV file, to see if the importing can be improved.
Réponse acceptée
Dyuman Joshi
le 19 Déc 2023
Déplacé(e) : Dyuman Joshi
le 19 Déc 2023
"The problem is date corresponding to 24hrs is not coverted in datetime."
Yes, because in 24 hour format, the hours are counted from 0 to 23.
A work around which works for the data in the given format -
load('data.mat')
format longg
date = datetime(string(data.Date - 1),'InputFormat','yyyyMMddHH')
4 commentaires
Fangjun Jiang
le 19 Déc 2023
If the data.Date in data.mat is the best format that you prefer, you could apply the same "duration" idea.
date = datetime(string(data.Date - 1),'InputFormat','yyyyMMddHH')+hours(1)
Plus de réponses (2)
Fangjun Jiang
le 19 Déc 2023
Modifié(e) : Fangjun Jiang
le 19 Déc 2023
You could treat the last two digits as duration. Code below is not optimized but gives the correct result.
load Data.mat;
date_data=char(string(data.Date));
a=char(date_data);
b=a(:,1:end-2);
c=a(:,end-1:end);
d=datetime(b,'InputFormat','yyyyMMdd')+hours(str2num(c))
02-Jan-2016 20:00:00
02-Jan-2016 21:00:00
02-Jan-2016 22:00:00
02-Jan-2016 23:00:00
03-Jan-2016 00:00:00
03-Jan-2016 01:00:00
Seth Furman
le 2 Jan 2024
Another solution is the numeric datetime constructor syntax (i.e. t = datetime(Y,M,D,H,MI,S)), which allows values outside the conventional range:
"If an element of the Y, M, D, H, MI, or S inputs falls outside the conventional range, then datetime adjusts both that element and the same element of the previous input."
Since your timestamps always have the same field widths, you can 1) extract each field, 2) convert to numeric, and 3) call the datetime constructor.
load data.mat
% Convert Date to string
data = convertvars(data, "Date", "string");
% Extract date and time fields
data = addvars(data, extractBetween(data.Date, 1, 4), Before=1, NewVariableNames="Year");
data = addvars(data, extractBetween(data.Date, 5, 6), After=1, NewVariableNames="Month");
data = addvars(data, extractBetween(data.Date, 7, 8), After=2, NewVariableNames="Day");
data = addvars(data, extractBetween(data.Date, 9, 10), After=3, NewVariableNames="Hour");
% Convert fields to numeric
data = convertvars(data, ["Year","Month","Day","Hour"], "double");
% Construct datetime
data = addvars(data, datetime(data.Year, data.Month, data.Day, data.Hour, 0, 0), Before=1, NewVariableNames="Datetime");
tail(data)
0 commentaires
Voir également
Catégories
En savoir plus sur Dates and Time dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!