Getting value of a COM Date object in MATLAB

1 visualización (últimos 30 días)
Jesper
Jesper el 23 de Oct. de 2013
Editada: Jesper el 7 de Nov. de 2013
I am working with a library that handles a set of COM objects in MATLAB. One of the COM objects returns a Date object/value. When this is requested in MATLAB, I get it as a string, formatted using the regional settings of the computer.
So, depending on the computer running it (regional settings), the date part may end up as:
yyyy-mm-dd
mm-dd-yyyy
dd-mm-yyyy
m/d/yyyy
d/m/yyyy
and the time part similarly:
HH:MM:SS
hh:MM:SS AM/PM
HH:MM:SS.SSS
hh:MM:SS.SSS AM/PM
Depending on whether the Date object contains a time part, a day part, or both, any variations of above may occur. Bottom line is that I have not been able to make a method, that correctly parses the date to a valid date-time vector in MATLAB
Is there another way to do this, that I have missed, that does not involve parsing a string in a regional format?
If not, it would be nice with some build-in method in MATLAB that can do this conversion (feature request...).

Respuesta aceptada

Friedrich
Friedrich el 23 de Oct. de 2013
Editada: Friedrich el 23 de Oct. de 2013
Hi,
since you are on Windows you can use .NET to get the current System setting:
settings = System.Globalization.DateTimeFormatInfo
After that you can access certain property which tell you how the date and time is formated, e.g.
>> settings.LongDatePattern
ans =
dddd, dd MMMM yyyy
>> settings.LongTimePattern
ans =
HH:mm:ss
>> settings.ShortDatePattern
ans =
MM/dd/yyyy
>> settings.ShortTimePattern
ans =
HH:mm
You can use this information to call the datevec function with the correct format specifier.
  1 comentario
Jesper
Jesper el 7 de Nov. de 2013
Editada: Jesper el 7 de Nov. de 2013
Thank you for the suggestion. The correct one to use seems to be
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern
That is at least the one that matches the output from the COM date on my PC.
It makes it possible to do the parsing, though it is still complicated due to string content being date with/without time or time alone - and very slow due to the text parsing stuff. But at least it gets me half way through...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Use COM Objects in MATLAB en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by