How can I parse regional date and time strings returned by Windows XP in MATLAB 7.6 (R2008a)?

3 views (last 30 days)
I am using a COM server which returns a date string to MATLAB using the regional settings of the Windows operating system. I would like to parse this string using the function DATEVEC without knowing the system's date and string format beforehand. For example, on certain computers, I might receive the following date and time string:
date = '5.6.2008';
time = '5*43*12 PM';

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The ability to parse a Windows XP generated date or time string based on regional settings is not available in MATLAB.
As a workaround, you can use the WINQUERYREG function to extract the regional date and time formats from the Windows XP registry and then convert the Windows format specifiers to MATLAB format specifiers using REGEXPREP as shown below. You can then use the converted format specifiers to parse Windows-generated strings using DATEVEC. Note that not all Windows formats have a MATLAB equivalent.
%Obtain Windows XP Regional date and time formats from the registry
timesep = winqueryreg('HKEY_CURRENT_USER','Control Panel\International','sTime');
datesep = winqueryreg('HKEY_CURRENT_USER','Control Panel\International','sDate');
timefmt = winqueryreg('HKEY_CURRENT_USER','Control Panel\International','sTimeFormat');
datefmt1 = winqueryreg('HKEY_CURRENT_USER','Control Panel\International','sShortDate');
datefmt2 = winqueryreg('HKEY_CURRENT_USER','Control Panel\International','sLongDate');
%Convert the formats specifiers to match those of MATLAB
mltimefmt=upper(timefmt);
mltimefmt=regexprep(mltimefmt,'H+','HH');
mltimefmt=regexprep(mltimefmt,'M+','MM');
mltimefmt=regexprep(mltimefmt,'S+','SS');
mltimefmt=regexprep(mltimefmt,'T+','AM');
mldatefmt1 = regexprep(lower(datefmt1),'(?<!d)d(?!d)','dd');
mldatefmt1 = regexprep(mldatefmt1,'(?<!m)m(?!m)','mm');
mldatefmt2 = regexprep(lower(datefmt2),'(?<!d)d(?!d)','dd');
mldatefmt2 = regexprep(mldatefmt2,'(?<!m)m(?!m)','mm');
%Replace single P or A with PM or AM
datef = regexprep(date, '(P|A)(?!M)','$1M');
timef = regexprep(time, '(P|A)(?!M)','$1M');
%Parse date and time strings using DATEVEC
datevec(datef, mldatefmt1);
datevec(timef, mltimefmt);
For more information on the WINQUERYREG function execute the following at the MATLAB command prompt
doc winqueryreg

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!