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

1 visualización (últimos 30 días)
German Barrera
German Barrera el 3 de Abr. de 2018
Respondida: BhaTTa el 14 de Oct. de 2024
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!

Respuestas (1)

BhaTTa
BhaTTa el 14 de Oct. de 2024
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.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by