Getting value of a COM Date object in MATLAB

1 visualizzazione (ultimi 30 giorni)
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...).

Risposta accettata

Friedrich
Friedrich il 23 Ott 2013
Modificato: Friedrich il 23 Ott 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 Commento
Jesper
Jesper il 7 Nov 2013
Modificato: Jesper il 7 Nov 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...

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by