How do I convert datetime in string format to Matlab datetime?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Felicia Ojinji
le 16 Nov 2020
Commenté : Steven Lord
le 16 Nov 2020
I have 8760 rows of data in this format '02/01/2019 09'. I would like to convert this to Matlabe datetime format. Thank you
0 commentaires
Réponse acceptée
Ameer Hamza
le 16 Nov 2020
Modifié(e) : Ameer Hamza
le 16 Nov 2020
Try following InputFormat
x = '02/01/2019 09';
dt = datetime(x, 'InputFormat', 'dd/MM/yyyy hh')
Result
>> dt
dt =
datetime
02-Jan-2019 09:00:00
3 commentaires
Steven Lord
le 16 Nov 2020
That text you posted, '02/01/2019 09'. Does it represent January 2nd, 2019 at 9:00 AM? Or does it represent February 1st, 2019 at 9:00 AM? If the latter you want to use MM/dd/yyyy hh'.
s = '02/01/2019 09'
J = datetime(s, 'InputFormat', 'dd/MM/yyyy hh')
F = datetime(s, 'InputFormat', 'MM/dd/yyyy hh')
Using the wrong format could lead to problems later on if you assumed one of the two potential formats by which s could be interpreted but a later entry is unambiguously in the other format, like:
datetime('20/01/2021', 'InputFormat', 'dd/MM/yyyy')
datetime('20/01/2021', 'InputFormat', 'MM/dd/yyyy') % Will error
Plus de réponses (0)
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!