How can I convert the number 1 into a date value 20150101 (yyyyMMdd)?
1 view (last 30 days)
Show older comments
How can I convert A:
A = [ 1 2 3 4 5 ... 731]
into
[ 20150101 20150102 20150103 20150104 20150105 ... 20161231]
0 Comments
Answers (3)
Jan
on 26 Sep 2017
Edited: Jan
on 27 Sep 2017
This works but is slow (see comments):
A = [ 1 2 3 4 5 731];
num = datenum('31-Dec-2014') + A;
vec = sscanf(datestr(num, 'yyyymmdd ').', '%d')
or faster:
mat = datevec(datenum('31-Dec-2014') + A);
vec = mat * [10000; 100; 1; 0; 0; 0]
[EDITED]
dt = datetime(2015, 1, A, 'Format', 'uuuuMMdd')
>> 1×6 datetime array
20150101 20150102 20150103 20150104 20150105 20161231
This looks similar to the wanted output, but it cannot be converted to a double vector directly.
4 Comments
Guillaume
on 27 Sep 2017
@Jan,
"This looks similar to the wanted output, but it cannot be converted to a double vector directly."
Of course, it can:
dt = datetime(2015, 1, A, 'Format', 'uuuuMMdd')
str2double(cellstr(dt))
By the way, I would recommend spelling fully parameter names so that newbies don't wonder what the 'F' option is.
@André
In Matlab, you can type doc something or help something to learn about something. It's the fastest way to learn about functions you don't know about. So, try:
doc tic
Jan
on 27 Sep 2017
@Guillaume: I meant directly. The format 'uuuuMMdd' does not convert the internal storage of the values, but concerns the output to strings only. Then cellstr(dt) is a complicated conversion already. str2double means some work also, because the conversion from a string to a double is surprisingly complicated to consider exceptions and care for replying the double which is as near as possible to the value represented in the string.
I do not know, how the date and time values are store internally: As serial date numbers or as date vectors. For the last case year(dt)*10000+... would be a "direct" conversion. But if date numbers are used, even cellstr(dt) means 2 conversions already to obtain the numerical values for year, month and day, and to create a string in the uuuuMMdd format.
Peter Perkins
on 27 Sep 2017
There's a datetime method for that:
>> A = (1:731)';
>> d = datetime(2015,1,A)
d =
6×1 datetime array
01-Jan-2015
02-Jan-2015
[snip]
30-Dec-2016
31-Dec-2016
>> ymd = yyyymmdd(d);
ymd =
20150101
20150102
[snip]
20161230
20161231
But ask yourself why you want that. You are most likely better off sticking with d, the datetime vector, unless you need text, in which case ...
>> string(d,'yyyMMdd')
ans =
731×1 string array
"20150101"
"20150102"
"20150103"
[snip]
... or unless you reaaly need those double values to hand off to some other function that only accepts dates in that form.
2 Comments
Jan
on 27 Sep 2017
+1. yyyymmdd()? Wow, this does really match the OP's needs. I will include it in the runtime comparison later.
Andrei Bobrov
on 26 Sep 2017
Edited: Andrei Bobrov
on 26 Sep 2017
out = datetime([2015 01 01],'F','uuuuMMdd') + A -1
or
x = datevec(datetime([2015 01 01]) + A -1);
out = x(:,1:3)*[10000;100;1];
0 Comments
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!