How can I convert the number 1 into a date value 20150101 (yyyyMMdd)?

1 次查看(过去 30 天)
How can I convert A:
A = [ 1 2 3 4 5 ... 731]
into
[ 20150101 20150102 20150103 20150104 20150105 ... 20161231]

回答(3 个)

Jan
Jan 2017-9-26
编辑:Jan 2017-9-27
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 个评论
Guillaume
Guillaume 2017-9-27
@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
Jan 2017-9-27
@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
Peter Perkins 2017-9-27
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.

Andrei Bobrov
Andrei Bobrov 2017-9-26
编辑:Andrei Bobrov 2017-9-26
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];

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by