0

I am trying to convert the following datetime into string format in Matlab.

20121003.03

It corresponds to 3rd Oct 2012, 03:00am. Any ideas?

1
  • What format is it in now? Is 20121003.03 stored as a floating point number? Commented Mar 2, 2018 at 16:51

1 Answer 1

1

You can specify a custom input format when creating a datetime object. Your question seems to indicate that the object is already a datetime object, but at that point the problem is trivial, just call datestr on it (the last line of the script below). I'll assume here that the input is a floating point representation.

dval = 20121030.0330;

% Convert floating point number to a string
dstr = sprintf('%013.04f', dval);

% Convert string to a datetime object
dtime = datetime(dstr, 'InputFormat', 'yyyyMMdd.hhmm');

% Convert the datetime object into a formatted string
dstr2 = datestr(dtime);

Output:

dstr2 = '30-Oct-2012 03:00:00'

If you want an output format different than the default then you need to specify the output format of datestr. See the documentation for more information.

Sign up to request clarification or add additional context in comments.

3 Comments

At the moment I'm reading tabular data in with load and it is stored as 'double'. Your suggestion worked perfectly for the example I gave. But it doesn't work for all my entries. For example, I also have: 20121003.033. I believe the data I have is every 30 mins.
modified based on comment.
Actually I think just having 'yyyyMMdd.hhmm' has fixed that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.