"Locale" issue. Danish created-date extracted from file

1 回表示 (過去 30 日間)
Steeven
Steeven 2018 年 5 月 17 日
コメント済み: Peter Perkins 2018 年 5 月 31 日
I am extracting the creation date of a file on my Windows 10 PC with the following code lines:
fileInfo = dir(filePath);
fileInfo.date
datetime(fileInfo.date)
fileInfo.date is written out while datetime fails. The result is:
As you can see the extracted created-date is given in a correct, readable format dd-mmm-yyyy hh:mm:ss. But the month is written in Danish language maj. I have not had this error before, most likely because the first 3 letters of all other Danish months are the same as in English. But in the glorious month of May it fails.
As the error message tells me, I have been looking into the Locale parameter, but I am not able to use it for my specific purpose (there are e.g. several English language questions like this one but I need a Danish conversion equivalent).
I am not able to change computer settings (that will be impractical in any case, as the script will be run on different computers now and then).
Can anyone give me a helping hand? Can MatLab do a quick DK-to-EN conversion while doing the datetime command? (Could MatLab maybe do such a conversion from any language, such that this will never be an issue for anyone using the script?)
Or must I do a string-search for Danish words and replace them with English? The following is namely working well - but is very unflexible and specific:
datetime(strrep(fileInfo.date,'maj','may'));

採用された回答

Stephen23
Stephen23 2018 年 5 月 17 日
編集済み: Stephen23 2018 年 5 月 17 日
Avoid the whole problem by using the structure's datenum field, which is a MATLAB serial datenumber. You can easily convert this to datetime using something like this:
S = dir(...)
T = datetime([S.datenum],'ConvertFrom','datenum')
  2 件のコメント
Steeven
Steeven 2018 年 5 月 17 日
編集済み: Steeven 2018 年 5 月 17 日
Aha, this works just fine and is a neat and simple solution. Thank you very much.
What is the difference between datetime and datenum? Is one carrying the timestamp in a "date-and-time" format and the other in a "seconds-since-some-beginning-of-time" format, or how?
Stephen23
Stephen23 2018 年 5 月 17 日
編集済み: Stephen23 2018 年 5 月 17 日
A serial datenumber is just a simple numeric (double) which counts the days since the date 0th of January in year 0000. In contrast datetime, duration, etc. are sophisticated objects which contain many useful methods, and which know about things like timezones, date formatting, leap years, etc.
Read more here:

サインインしてコメントする。

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2018 年 5 月 17 日
Specifying the locale would work:
>> datetime('11-maj-2018 15:39:22','Locale','da_DK')
ans =
datetime
11-May-2018 15:39:22
But as Stephen says, using the datenum output from dir avoids the problem entirely.
  3 件のコメント
Walter Roberson
Walter Roberson 2018 年 5 月 17 日
Watch out for timezone issues either way.
Peter Perkins
Peter Perkins 2018 年 5 月 31 日
It is possible to use ,'Locale','system', but that may also be fragile to the specifics the system settings of who's using it.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by