Convert double to DateTime format?

How to convert numeric double types into datetime 'dd/mm/yyyy hh.mm.ss' format? Example: 20180304122050 -> become 04/03/2018 12:20:50

6 Comments

Stephen23
Stephen23 on 5 Jun 2018
Edited: Stephen23 on 5 Jun 2018
@Fredrick Neo: you are going to have to be more precise telling us what you have, and what you want.
While the number 20180304122050 could be encoded as a single double number without loss of precision, this would be a very unusual encoding (I have never seen anyone do this with second precision). Typical floating point date encoding simply counts the second or days from some epoch (e.g. MATLAB's datenum, or POSIX time). How is that value really stored?
Your requested output seems to be a string, rather than datetime object: do you want a string with a particular format, or a datetime object?
Please explain with more detail what you are trying to do.
Use the 'ConvertFrom' option
Would be great if datetime recognized more than 'yyyymmdd' precision, Walter, but afaict it won't...any field beyond that returns error "Unrecognized format" and the internal code for the conversion is
case 'yyyymmdd'
if any(value(:) < 0)
error(message('MATLAB:datetime:YYYYMMDDOutOfRange'));
end
% Let the month and day numbers roll just as they would for datevecs
value = double(full(value));
year = round(value/10000);
value = value - year*10000;
month = round(value/100);
day = value - month*100;
t_data = matlab.internal.datetime.createFromDateVec({year month day},'');
thru R2016b and current doc doesn't indicate any difference in input.
"Would be great if datetime recognized more than 'yyyymmdd' precision..."
I am not convinced that this would be good at all: encoding dates like this is an abuse of the double class, because the actual numeric values bear no direct relationship to the chronological. It does not have the advantages of serial date numbers (e.g. differences, addition, subtraction), nor any of the advantages of dates vectors (e.g. incrementing by specific units). Any calculations require extracting the digits, which is fiddly at the best of times.
Is OP not just asking for this?
x = num2str(20180304122050);
date = datetime(x,'InputFormat','yyyyMMddhhmmss','Format','dd/MM/yyyy hh:mm:ss');
date:
04/03/2018 12:20:50
dpb
dpb on 5 Jun 2018
Edited: dpb on 5 Jun 2018
Paolo -- looks like probably simplest solution to code; can do without the temporary even in actual implementation.
Stephen -- I meant "great" in the sense of the conversion would be trivial to code if the additional formatting strings were parsed; not so much that it is a great encoding scheme. Altho I will say I have done such things in previous life when working in constrained memory systems where the storage between 8 and 12/14 bytes was important--that's been almost 40 yr ago by now, though, granted! :)

Sign in to comment.

Answers (0)

Categories

Tags

Asked:

on 5 Jun 2018

Edited:

dpb
on 5 Jun 2018

Community Treasure Hunt

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

Start Hunting!