Can't get string to datetime conversion to work

There seems to be some sort of inconsistency in the converting to datetime code I missed.
q=datetime('2017-05-17 19:36:00', 'InputFormat','yyyy-MM-dd HH:mm:ss')
this works just fine. However, when I am reading the input string '2017-05-17T19:36:00' from an array, stripping the T, and putting it in exactly the same, it fails to work. example:
%data{1}(n) has the string '2019-05-17T19:36:00'
b = strrep(data{1}(n),'T',' ');
disp(b);
%displaying displays 2017-05-17 19:36:00, just like the earlier statement
timestamp = datetime(b,'InputFormat',format);
When I do this, the error message:
Unable to convert '2017-05-17 19:36:00' to datetime using the format 'yyyy-MM-dd HH:mm:ss'.
is displayed. Am i missing something? are these not exactly the same? Thanks for your help.

2 comentarios

could you attach data{1} as a mat file so we can check for odd characters?
Nathan Heller
Nathan Heller el 11 de Jun. de 2021
here you go

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 11 de Jun. de 2021

0 votos

The very first entry in your data{1} has a leading U+FEFF character, which is a unicode Byte Order Mark character, but is to be interpreted as a Zero-Width Non-Breaking Space if it occurs anywhere other than the first position

3 comentarios

Nathan Heller
Nathan Heller el 12 de Jun. de 2021
how would I go about removing that programmatically? or does it not appear multiple times and i only need to remove it once?
It probably only occurs once, but to be sure, use another strrep to replace char(65279) with empty
Nathan Heller
Nathan Heller el 12 de Jun. de 2021
Thanks for all your help!

Iniciar sesión para comentar.

Más respuestas (1)

str = '2019-05-17T19:36:00';
fmt = 'yyyy-MM-dd HH:mm:ss';
b = strrep(str,'T',' ');
disp(b);
2019-05-17 19:36:00
timestamp = datetime(b,'InputFormat',fmt)
timestamp = datetime
17-May-2019 19:36:00
Works for me ?

4 comentarios

Nathan Heller
Nathan Heller el 11 de Jun. de 2021
I still get the same error when i use your code with str = data {1}(n), so i assume there must be some problem with the data file?
Jim Benjamin
Jim Benjamin el 11 de Mzo. de 2025
Hi everyone,
I have a similar problem: I like to convert the string
s(1) = "11-Mrz-2025 10:03:47" to datetime using
s1 = datetime(s(1), 'InputFormat', 'dd-MMM-yyyy HH:mm:ss', 'Locale', 'de_DE') and I get the error
Error using datetime (line 257)
Unable to convert '11-Mrz-2025 10:03:47' to datetime using the format 'dd-MMM-yyyy hh:mm:ss' and locale 'de_DE'.
I do not know why this Input Format is not working.
Looks like Mrz is not the "official" short literal month name for March in the de_DE localization
str_en = "11-Mar-2025 10:03:47"
str_en = "11-Mar-2025 10:03:47"
dt = datetime(str_en)
dt = datetime
11-Mar-2025 10:03:47
string(dt,'dd-MMM-uuuu HH:mm:ss',"de_DE")
ans = "11-März-2025 10:03:47"
str = ["11-Mai-2025 10:03:47","11-Okt-2025 10:03:47","11-März-2025 10:03:47"]
str = 1x3 string array
"11-Mai-2025 10:03:47" "11-Okt-2025 10:03:47" "11-März-2025 10:03:47"
datetime(str, "InputFormat", "dd-MMM-uuuu HH:mm:ss", "Locale", "de_DE")
ans = 1x3 datetime array
11-May-2025 10:03:47 11-Oct-2025 10:03:47 11-Mar-2025 10:03:47
Walter Roberson
Walter Roberson el 11 de Mzo. de 2025
Editada: Walter Roberson el 11 de Mzo. de 2025
de_DE also uses 'Juni' and 'Juli' instead of 'Jun' and 'Jul'
From a 2017 posting of mine:
Marz = char([77 228 114 122]); %'März' -- but protect in case your system only uses 7 bit characters for .m files
date_strings = regexprep(date_strings, {'Mrz', 'Jun(?=\W)', 'Jul(?=\W)'}, {Marz, 'Juni', 'Juli'}, 'ignorecase');
dates = datetime(date_strings,'InputFormat', 'eee MMM d HH:mm:ss yyyy', 'locale', 'de_DE');

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Etiquetas

Preguntada:

el 11 de Jun. de 2021

Editada:

el 11 de Mzo. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by