How to remove the H:M:S in datetime variables when "writetable" to Excel

27 visualizaciones (últimos 30 días)
Hi! I find a problem in my datetime variables when writing my table results into Excel via "writetable".
  • I would like to keep the D/M/Y format (e.g. 15/03/2008) in the table, but I finally find that the Excel file shows the D/M/Y H:M:S format (e.g. 15/03/2008 12:00:00 AM).
I am wondering how can I fix this problem --- so that the Excel file can show the original datetime format (without H:M:S) as in MATLAB.
Thanks a lot for your help!

Respuesta aceptada

Vinesh Katewa
Vinesh Katewa el 12 de Jul. de 2021
Hi Jiashun Wu,
I understand that you want the output in D/M/Y format and not with the H:M:S which is the default output of the function. To get the output of datetime function in a specific format, you can use the 'Format' name value pair with datetime function as follows:
>> T = datetime();
>> D = datetime(T, 'Format', 'dd/MM/yyyy');
Hope this helps.
  3 comentarios
Vinesh Katewa
Vinesh Katewa el 12 de Jul. de 2021
Dear Jiashun,
I replicated your steps and found out that the output in excel is in desired format while using csv format but otherwise while using xlsx format.
For example, for a table defined as below:
t = datetime();
t = datetime(year(t), month(t), day(t), 'Format', 'dd/MM/yyyy');
T = table([t; t; t]);
Following code outputs desired results:
writetable(T, 'mydata.csv', 'Delimiter', ',')
But the following gives the '12:00:00 AM' output with the time
writetable(T, 'mydata.xlsx', 'WriteMode', 'replacefile')
This is happening due to the default date and time formatting in xlsx files in Microsoft excel.
Please note that using different programs instead of excel I am able to see the date time in desired format in both extensions.
In conclusion, you can either change the default date time format in excel for your datetime column or change the file extension.
Hope this helps to resolve your query.
Jiashun Wu
Jiashun Wu el 12 de Jul. de 2021
Dear Vinesh,
Thanks for this helpful comment!
Best regards,
Jiashun

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by