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

1 回表示 (過去 30 日間)
German Barrera
German Barrera 2018 年 4 月 3 日
回答済み: BhaTTa 2024 年 10 月 14 日
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 Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by