Can I set a datetime locale for readtable in a script?

7 ビュー (過去 30 日間)
dormant
dormant 2025 年 2 月 12 日
コメント済み: dormant 2025 年 2 月 12 日
I am having problems reading datetimes with readtable. This is on a set of csv files where the date and time formats differ slightly. (I have no control over the file format.)
One file might have:
12/01/2024 01:00:05, 1
Another file might have:
12/1/2024 1:00:05, 1
All dates are in UK-style format. My default locale (on Linux) is en_US.utf8.
When I use readtable, MATLAB correctly parses the date format as being UK-style for most of the files. But for some files, it uses the US format. This if for files where the day number never goes above 12.
I don't want to change the default Linux locale, or the default MATLAB locale, because I have existing scripts that read dates in US-style format.
How can I change this one script to force MATLAB to use UK-style dates? Specifying the format as "dd/MM/yyyy :hh:mm:ss" doesn't seem to work because of the lack of zero-padding at times.

採用された回答

Star Strider
Star Strider 2025 年 2 月 12 日
Note that "dd/MM/yyyy :hh:mm:ss" uses 12-hour format (using hh for the hours) without the AM/PM indicator. To use 24-hour format, use HH for the hours.
If the AM/PM indicator is the ‘1’ it might be necessary to programatically change that (using strrep or something similar), since I cannot find any reference to it in the documentation.
  3 件のコメント
Walter Roberson
Walter Roberson 2025 年 2 月 12 日
datetime(["12/1/2024 1:00:05", "12/01/2024 01:00:05"], 'InputFormat', 'dd/MM/yyyy HH:mm:ss')
ans = 1x2 datetime array
12-Jan-2024 01:00:05 12-Jan-2024 01:00:05
dormant
dormant 2025 年 2 月 12 日
Thanks. This sorted my problem.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by