how to convert '10:00 AM' to datetime?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Yu Li
el 16 de Feb. de 2022
Respondida: Walter Roberson
el 17 de Feb. de 2022
Hi:
I have a string '10:30 AM', I want to convert it to time using 'datetime' but failed, is there any way to do that?
I tried to specify the format using command below but still failed:
datetime('10:30 AM','InputFormat','HH:MM')
Thanks!
Yu
0 comentarios
Respuesta aceptada
Walter Roberson
el 17 de Feb. de 2022
datetime('10:30 AM','InputFormat','hh:mm a')
Notice how the current day is automatically used. MATLAB does not have any datatype for pure clock time without a date: it has datetime (that can be configured to display nor not display the date or seconds or whatever even though they are stored); and it has duration() objects that are purely relative objects with no date or absolute hour, just elapsed time.
You can do
temp = datetime('10:30 AM','InputFormat','hh:mm a')
day_relative = temp - dateshift(temp, 'start', 'day')
and now you have a duration relative to the beginning of the day.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!