How to convert Data from 'yyyyMMddhhmm' format to datetime format
조회 수: 23 (최근 30일)
이전 댓글 표시
Hello
I have been searching for a solution to converting my data from a number value with a 'yyyyMMddhhmm' format to a datetime format. Previously i attempted the following:
dtm = datetime(HS_Date,'InputFormat','yyyyMMddHHmm');
where HS_date is a matrix of 1x52561 double values, the data is from 202201010000 to 202301010000.
But i get an error message saying the following: "Error using Datetime (line 588) Numeric input data must be a matrix with three or six columns, or else three, six, or seven separate numeric arrays. You can also create datetimes from a single numeric array using the 'ConvertFrom' parameter."
The ConvertFrom parameter only offers the yyyMMdd format and is not an viable option.
I am unsure if i am defining anything wrongly, or if i need to format my data differently to make the function work.
note: my data is showcased in the program as 202201010000.000, but i cannot change or remove the decimals using the round or fix function.
Kind regards
댓글 수: 0
채택된 답변
Stephen23
2021년 11월 9일
편집: Stephen23
2021년 11월 9일
"... HS_date is a matrix of 1x52561 double values, the data is from 202201010000 to 202301010000."
Abuse of decimal numbers to represent date/time values in entirely different bases that coincidentally happen to have the same digits... ugh, Ugh, UGH. The best solution would be to store/import that data correctly.
But that unnatural data can be converted to DATETIME with a little bit of effort:
N = [202201010000;202301010000]; % very badly designed data.
D = datetime(compose("%d",N),'InputFormat','yyyyMMddHHmm')
If you only need to date part then you could use the inbuilt ConvertFrom:
D = datetime(N/1e4,'ConvertFrom','yyyymmdd')
추가 답변 (1개)
Chunru
2021년 11월 9일
dstr = ["202201010000.000" "202301010000.000"]
t = datetime(dstr,'InputFormat','yyyyMMddHHmm.SSS')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!