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

26 vues (au cours des 30 derniers jours)
Jiashun Wu
Jiashun Wu le 12 Juil 2021
Commenté : Jiashun Wu le 12 Juil 2021
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!

Réponse acceptée

Vinesh Katewa
Vinesh Katewa le 12 Juil 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 commentaires
Vinesh Katewa
Vinesh Katewa le 12 Juil 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 le 12 Juil 2021
Dear Vinesh,
Thanks for this helpful comment!
Best regards,
Jiashun

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by