Specific timestamp to date format using datetime

23 ビュー (過去 30 日間)
André Bernal García
André Bernal García 2017 年 3 月 27 日
コメント済み: Star Strider 2017 年 3 月 28 日
I have data with a certain timestamp, being yyyymmddHHMM and I want to convert this to time format in matlab:
For example:
Given a timestamp of 201502171000 I want to get 2015-02-17 10:00.
PS: I have to manage a lot of data for a given time unit and my aim is to identify my measurements for that certain time unit. And thats why I thought that time format could be helpful.

採用された回答

Star Strider
Star Strider 2017 年 3 月 27 日
This works:
T = 201502171000;
str = num2str(T, '%11d');
dn = datenum(str, 'yyyymmddHHMM');
ds = datestr(dn, 'yyyy-mm-dd HH:MM') % ‘Legacy’ Functions
dt = datetime(str, 'InputFormat','yyyyMMddHHmm', 'Format','yyyy-MM-dd HH:mm') % ‘datetime’ Function
I do not know if you are starting with an integer or string, so I included the conversion from integer to string, necessary for the datetime function.
  2 件のコメント
André Bernal García
André Bernal García 2017 年 3 月 28 日
Thanks! Now I have another problem, I have the time array and a data array.
My purpose is to make a loop as:
For each hour ( the data time is hourly), so for 2015-02-17 10:00, 2015-02-17 11:00 and so on
Use the values of the array UP ( in the picture) to do certain mathematical operations
Thanks for all in advance!
Star Strider
Star Strider 2017 年 3 月 28 日
My pleasure.
I am not certain what you want to do.
If you want to put your datetime object and your data together, I would use the table function, since it is designed for that. Another option is a cell array.

サインインしてコメントする。

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2017 年 3 月 27 日
Assuming you have strings, not numbers, you've kind of answered the question yourself, except for m vs. M:
>> datetime('201502171000','InputFormat','yyyyMMddHHmm')
ans =
datetime
17-Feb-2015 10:00:00

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by