convert dates to specific format

how can I change the format of the dates in the column shown in the image to the format uuuuMMddHHmm?

回答 (2 件)

Stephen23
Stephen23 2022 年 7 月 19 日
編集済み: Stephen23 2022 年 7 月 19 日

1 投票

My guess is that you have unfortunately stored each date as one numeric value, whose digits just happen to coincide with the date units' digits. This is rather a misuse of numerics, and is best fixed by importing that data correctly, rather than trying to "fix" it later. But you can convert to a proper DATETIME object if required:
N = 200701022359 % ugh
N = 2.0070e+11
D = datetime(string(N),'Format','uuuuMMddHHmm')
D = datetime
200701022359
Checking:
D.Year
ans = 2007
D.Day
ans = 2
D.Minute
ans = 59
Note that you should avoid deprecated functions DATENUM, DATESTR, and DATEVEC.
KSSV
KSSV 2022 年 7 月 19 日

0 投票

t0 = datenum(today) % probably you have datenums
t0 = 738721
t1 = datetime(datestr(t0)) % convert them to datetime
t1 = datetime
19-Jul-2022
t1.Format % check the format
ans = 'dd-MMM-uuuu'
t1.Format = 'uuuuMMddHHmm' % change the format
t1 = datetime
202207190000

3 件のコメント

Salma fathi
Salma fathi 2022 年 7 月 19 日
Thank you for the quick reply.
When applying the provided lines I get the following error
DATESTR failed converting date number to date vector.
Error in datestr (line 202)
S = dateformverify(dtnumber, dateformstr, islocal);
Error in cateogorizing (line 337)
t1 = datetime(datestr(RO_data.Date)); % convert them to datetime
Caused by:
Error using datevecmx
Date number out of range.
Any advice how to fix it?
Stephen23
Stephen23 2022 年 7 月 19 日
"Any advice how to fix it?"
  1. understand how your date is currently stored.
  2. use that knowledge to select a suitable tool to convert it to DATETIME.
KSSV
KSSV 2022 年 7 月 19 日
Attach your data.

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

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

製品

リリース

R2021b

タグ

質問済み:

2022 年 7 月 19 日

コメント済み:

2022 年 7 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by