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

22 ビュー (過去 30 日間)
Jiashun Wu
Jiashun Wu 2021 年 7 月 12 日
コメント済み: Jiashun Wu 2021 年 7 月 12 日
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!

採用された回答

Vinesh Katewa
Vinesh Katewa 2021 年 7 月 12 日
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 件のコメント
Vinesh Katewa
Vinesh Katewa 2021 年 7 月 12 日
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 2021 年 7 月 12 日
Dear Vinesh,
Thanks for this helpful comment!
Best regards,
Jiashun

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by