convert datetime format 01-feb-2022 to 2022-02-01 in matlab

You can use the datetime, datestr, and datenum functions in MATLAB to convert the format of a datetime.

Here's how you can convert a datetime from the format "01-feb-2022" to "2022-02-01":

main.m
% Input datetime string in the original format
originalDatetimeString = '01-feb-2022';

% Convert the original datetime string to a datetime object
originalDatetime = datetime(originalDatetimeString, 'InputFormat', 'dd-MMM-yyyy');

% Convert the datetime object to a new format
newDatetimeString = datestr(originalDatetime, 'yyyy-mm-dd');
339 chars
9 lines

The datetime function is used to convert the original datetime string to a datetime object. The 'InputFormat' option is used to specify the format of the original datetime string.

The datestr function is then used to convert the datetime object to the desired format. The 'yyyy-mm-dd' format specifier is used to output the date as "YYYY-MM-DD".

The variable newDatetimeString will then contain the datetime string in the desired format "2022-02-01".

related categories

gistlibby LogSnag