they both work here (i also replaced deprecated datestr with the recommended char).
note that you should use fullfile instead of concatenating text together.
actual_time = char(datetime('now'))
s1 = dir([actual_time '\*.txt']) % should use fullfile here
actual_time = fullfile(replace(actual_time, ':', '-'))
s2 = dir([actual_time '\*.txt']) % should use fullfile here
"is there any other way to do this?"
explicitly specify the datetime format:
dt = datetime('now','format','uuuu-mm-dd hh-mm-ss')
s = dir(fullfile(char(dt),'*.txt'))