How can I create a new column with date info with specific date format ?

1 次查看(过去 30 天)
Hi everyone
I need to make a new column with date info from a file like the one below, but with this format:
20180101.000000
20180101.060000
%year mm dd hh
1997 1 1 0
1997 1 1 6
1997 1 1 12
1997 1 1 18
1997 1 2 0
1997 1 2 6
Thank you for taking the time, any help is appreciated!

回答(1 个)

BhaTTa
BhaTTa 2024-10-14
I assume that you have date saved in the format below
%year mm dd hh
1997 1 1 0
and you want to convert it into the format below
20180101.000000
Below I have attached an example code to achieve it
data = [
1997 1 1 0
1997 1 1 6
1997 1 1 12
1997 1 1 18
1997 1 2 0
1997 1 2 6
];
formattedDates = cell(size(data, 1), 1);
for i = 1:size(data, 1)
year = data(i, 1);
month = data(i, 2);
day = data(i, 3);
hour = data(i, 4);
% Format the date and time into the desired string format
formattedDates{i} = sprintf('%04d%02d%02d.%02d0000', year, month, day, hour);
end
disp(formattedDates);
Hope it helps.

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by