convert a string into a date and time format

Hello, How can I convert this string into a date and time format in matlab. The months will be in spanish and the number of letters may vary for the month. This data will be obtained from different files and it will vary.
m ='October17,196616:41:00'
The result should be date_time= 1996/10/17 16:41:00
Thank you

 采纳的回答

Try something like this —
m ='October17,196616:41:00';
DT = datetime(m, 'InputFormat','MMMMdd,yyyyHH:mm:ss', 'Format','yyyy/MM/dd HH:mm:ss')
DT = datetime
1966/10/17 16:41:00
.

4 个评论

@Jim Benjamin — One problem is that "hh" is used for 12-hour time, and has to have an AM/PM indicator used with it. Use "HH" for 24-hour time.
Correcting that, when I try the same approach using local (English) notation, it works —
q(1) = "11-Mar-2025 10:03:47"
q = "11-Mar-2025 10:03:47"
Q1 = datetime(q(1), "InputFormat","dd-MMM-yyyy HH:mm:ss")
Q1 = datetime
11-Mar-2025 10:03:47
However, I couild not get yourrs to work. I initially thought it might have something to do with the month abbreviation (I am not familiar with German month abbreviations, and my German generally is a bit atrophied), however when I looked that up and changed it (to Mär), it still did not work, however changing it to März does work. Using Mrz is apparently non-standard, at least as far as MATLAB (and that is an imoportant consideration here).
s(1) = "11-März-2025 10:03:47"
s = "11-März-2025 10:03:47"
s1 = datetime(s(1), 'InputFormat', 'dd-MMM-yyyy HH:mm:ss', 'Locale', 'de_DE')
s1 = datetime
11-Mar-2025 10:03:47
s1 = datetime(s(1), 'InputFormat', 'dd-MMM-yyyy HH:mm:ss', 'Format','yyyy MM dd HH:mm:ss', 'Locale', 'de_DE')
s1 = datetime
2025 03 11 10:03:47
So to fix this for your entire array, I suggest something like this —
s(1) = "11-Mrz-2025 10:03:47"
s = "11-Mrz-2025 10:03:47"
s = strrep(s, "Mrz","März")
s = "11-März-2025 10:03:47"
s1 = datetime(s(1), "InputFormat", "dd-MMM-yyyy HH:mm:ss", "Locale", "de_DE")
s1 = datetime
11-Mar-2025 10:03:47
Experiment with that approach. It might be necessary to change other non-standard abbreviations as well. See the documentation for strrep if you need to change more than one of them. You can store the options in matching vectors (for string arrays).
I had to search for this information, and it pulled up:
  • The abbreviations for the months in German are as follows: Jan (Januar), Feb (Februar), Mär (März), Apr (April), Mai (Mai), Jun (Juni), Jul (Juli), Aug (August), Sep (September), Okt (Oktober), Nov (November), and Dez (Dezember). Note that "Mai" and "März" are not abbreviated due to their short length.
These apparently are also the abbreviations that MATLAB recognises (I cannot find a specific list of them in the documentation).
.
Dear Star Strider,
Thanks a lot, with “März” instead of Mrz it works. So I have to rename every abbreviated name of the month to the full German name whe3n I use the Locale ‘de_DE’ . Thanks a million!
Hartmut
If you've imported the data as a string array:
s1 = ["11-Mrz-2025 10:03:47"; "12-Dec-2025 09:22:38"]
s1 = 2x1 string array
"11-Mrz-2025 10:03:47" "12-Dec-2025 09:22:38"
s2 = replace(s1, ["Mrz", "Dec"], ["März", "Dez"])
s2 = 2x1 string array
"11-März-2025 10:03:47" "12-Dez-2025 09:22:38"
d2 = datetime(s2, Locale="de_DE")
d2 = 2x1 datetime array
11-Mar-2025 10:03:47 12-Dec-2025 09:22:38
You could write a small helper function, call it standardizeMonth, to perform that replace call to replace any non-standard abbreviations you use.
@Jim Benjamin — You don’t have to rename all of them, only the non-standard ones.
Also, as I suggested and that @Steven Lord provided an example for, renaming the ones that don’t match MATLAB’s set (see my earlier Comment) can be done in one line using the strrep function.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Dates and Time 的更多信息

产品

版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by