How to convert date variable with varying length?
古いコメントを表示
Hi there, I have a date variable with thousands of dates in the format mmddyy i would like to convert it to a yyyymmdd format. A problem i am having is whenever the month is bellow 10 the 0 is missing e.g. rather than reading 012988 it reads 12988. Thank-you
1 件のコメント
Fangjun Jiang
2016 年 2 月 12 日
where is your source data come from, a text file, or already stored in a matrix, in what format?
採用された回答
その他の回答 (2 件)
Matthew Eicholtz
2016 年 2 月 12 日
If your data is stored in a cell array, such as
d = {'21216','112515','91101','122515','70487'}; %random dates in mmddyy format (with leading '0' missing)
then add the leading '0' to months Jan-Sep,
d = cellfun(@(x) sprintf('%06s',x),d,'uni',0)
and use the built-in date functions to convert to the desired format,
newdates = num2cell(datestr(datenum(d,'mmddyy'),'yyyymmdd'),2);
2 件のコメント
aaron Harvey
2016 年 2 月 13 日
Jan
2016 年 2 月 13 日
@Aaron: You see that it would have been useful to post some example data in the question to clarify the type.
Azzi Abdelmalek
2016 年 2 月 12 日
You have to indicate the format of your data. Suppose your data are like below:
d=[12988 112589 52214]
e=arrayfun(@(x) num2str(x),d,'un',0)
f=cellfun(@(x) [num2str(zeros(1,6-numel(x))) x],e,'un',0)
out=cellfun(@(x) datestr(datenum(x,'mmddyy'),'yyyymmdd'),f,'un',0)
カテゴリ
ヘルプ センター および File Exchange で Calendar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!