# print a date and time in command window MATLAB [closed]

I use matlab R2015a, when I run my script I want print the date and the time of exection

I tried this code

Dtimes=datetime('now','TimeZone','local','Format','d-MMM-y HH:mm:ss Z');
% the first test
disp(' Date and time is ' Dtimes ); % But not work
% second test
disp(sprintf('date and time is %s  ',Dtimes)) %also not work


But with this command is worked :

disp(sprintf('date and time is %s  ',datestr(now)));


What is the problem?

## closed as off-topic by jojek♦, MBaz, lennon310, Matt L., Peter K.♦Jul 9 '15 at 12:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "General programming questions are off-topic here, but can be asked on Stack Overflow." – jojek, MBaz, lennon310, Matt L., Peter K.
If this question can be reworded to fit the rules in the help center, please edit the question.

• How is that connected with signal processing? – jojek Jul 8 '15 at 8:47

While significantly off topic, this one is easy: Dtimes is datetime object and not a string. What would work is

display(Dtimes)
disp([' Date and time is ' char(Dtimes)] );


Based on Mathworks' documentation here, datetime returns an array, not a string. Therefore, you cannot use disp to display a string Date and time is together with that returned array.

Your second method is also not working because again Dtimes is an array and you cannot use %s to print it.

Your last method is working because datestr returns a string see documentation.

Please let me know if this explanation is not clear/enough.