converting UTC time formatted as 'ddd:HH:mm:ss.SSSSSSSS' from a string to a date time.
28 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Tim Starbuck
el 9 de Mzo. de 2022
Comentada: Tim Starbuck
el 14 de Mzo. de 2022
I have an array to strings that I need converted to a date time, but they are in an unusual format. The timezone is UTC, but the format of the date doesnt include the year. I do have the option of having the user input the the year or parsing it from another place. However the format of the time is 'ddd:HH:mm:ss.SSSSSSSSS', and I am struggling to figure out the right set of datetime comands in to convert it out of a string.
example:
User Input: Current year = 2022
UTC String: 036:19:45:30.123581351
Date: 02-05-2022 07:45:30.123581351 pm
Help would be appriciated! Thanks!
1 comentario
Respuesta aceptada
Stephen23
el 9 de Mzo. de 2022
Editada: Stephen23
el 9 de Mzo. de 2022
A = '2022';
B = '036:19:45:30.123581351';
C = sprintf('%s:%s',A,B);
D = datetime(C,'InputFormat','u:D:H:m:s.SSSSSSSSS')
You can adjust the display format by specifying the 'Format' property. Note that changing the 'Format' makes absolutely no difference to the date/time stored in memory, only to how it looks when displayed.
4 comentarios
Peter Perkins
el 10 de Mzo. de 2022
Editada: Peter Perkins
el 10 de Mzo. de 2022
I'm not sure I fully understand the question. Setting the year property is "as if" you got all six y/mo/d/h/mi/s components, changed the year, and recreated. The clockface stays the same except for the year:
>> d = datetime(2022,2,27:31)
d =
1×5 datetime array
27-Feb-2022 28-Feb-2022 01-Mar-2022 02-Mar-2022 03-Mar-2022
>> d.Year = 2023
d =
1×5 datetime array
27-Feb-2023 28-Feb-2023 01-Mar-2023 02-Mar-2023 03-Mar-2023
And if you did not start out with a 29 Feb, you won't end up with one if you set to a leap year:
>> d.Year = 2020
d =
1×5 datetime array
27-Feb-2020 28-Feb-2020 01-Mar-2020 02-Mar-2020 03-Mar-2020
>> caldiff(d,"days")
ans =
1×4 calendarDuration array
1d 2d 1d 1d
I think you are asking, "yeah, but what happens to 29 Feb if I set a leap year to a non-leap year?" There are several ways to specify a non-existent date or time, and that has to be handled. In this case "29-Feb-2022" gets rolled into 1-Mar-2022:
>> d = datetime(2020,2,27:31)
d =
1×5 datetime array
27-Feb-2020 28-Feb-2020 29-Feb-2020 01-Mar-2020 02-Mar-2020
>> d.Year = 2022
d =
1×5 datetime array
27-Feb-2022 28-Feb-2022 01-Mar-2022 01-Mar-2022 02-Mar-2022
>> caldiff(d,"days")
ans =
1×4 calendarDuration array
1d 1d 0d 1d
There's no right answer here, calendar arithmetic is messed up and there's nothing to be done about that.
Más respuestas (0)
Ver también
Categorías
Más información sobre Calendar 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!