DateTime YYYYMMDD HHMMSS ConvertTo

23 ビュー (過去 30 日間)
em_++
em_++ 2021 年 4 月 29 日
回答済み: Ayush 2024 年 7 月 9 日
Hi everyone,
i have a question:
i have a .txt file with two collums, which are the date and the time in this format: YYYYMMDD HHMMSS
now i want to convert it to this format DD-MM-YYYY HH:MM:SS
I already managed to convert the date but not the time:
date = datetime(File.txt,'ConvertFrom','yyyymmmdd')
output:
03-Feb-2021 00:00:00
thanks for your help!!

回答 (1 件)

Ayush
Ayush 2024 年 7 月 9 日
Hi,
To convert the data into the required format, you can make use of the "Format" property of the "datetime" object and set it to the desired output format. Refer to the example code below for a better understanding:
% Example date and time in the format YYYYMMDD HHMMSS
originalDateTime = '20231005 123456';
% Convert the string to a datetime object with the specified input format
dt = datetime(originalDateTime, 'InputFormat', 'yyyyMMdd HHmmss');
% Set the desired output format
dt.Format = 'dd-MM-yyyy HH:mm:ss';
% Convert the datetime object to a string
formattedDateTime = char(dt);
% Display the result
disp(formattedDateTime);
05-10-2023 12:34:56
For more information on the "datetime" function, refer to the below documentation:

カテゴリ

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