i want datetime in this format :
01/02/2008 501 (day/month/years and time)
i try it :
bbb=datetime(bb,"InputFormat", "dd/MM/yyyy");
but i get error format

1 件のコメント

Cris LaPierre
Cris LaPierre 2024 年 5 月 13 日
Based on your graphic, 01/02/2008 501 is actually (month/day/year time)

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

 採用された回答

Voss
Voss 2024 年 5 月 13 日

1 投票

load matlab_A
A
A = 1000x4
2008 1 2 501 2008 1 2 502 2008 1 2 503 2008 1 2 504 2008 1 2 505 2008 1 2 506 2008 1 2 507 2008 1 2 508 2008 1 2 509 2008 1 2 510
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
hh = floor(A(:,4)/100);
mm = mod(A(:,4),100);
ss = zeros(size(A,1),1);
B = [A(:,[1 2 3]) hh mm ss];
D = datetime(B,'Format','dd/MM/yyyy Hmm')
D = 1000x1 datetime array
02/01/2008 501 02/01/2008 502 02/01/2008 503 02/01/2008 504 02/01/2008 505 02/01/2008 506 02/01/2008 507 02/01/2008 508 02/01/2008 509 02/01/2008 510 02/01/2008 511 02/01/2008 512 02/01/2008 513 02/01/2008 514 02/01/2008 515 02/01/2008 516 02/01/2008 517 02/01/2008 518 02/01/2008 519 02/01/2008 520 02/01/2008 521 02/01/2008 522 02/01/2008 523 02/01/2008 524 02/01/2008 525 02/01/2008 526 02/01/2008 527 02/01/2008 528 02/01/2008 529 02/01/2008 530

6 件のコメント

shamal
shamal 2024 年 5 月 13 日
thank.... it's also fast
Voss
Voss 2024 年 5 月 14 日
You're welcome!
shamal
shamal 2024 年 5 月 20 日
hi, it's possible to avoid insert second?
B = [A(:,[1 2 3]) hh mm];
how can i code this : D = datetime(B,'Format','dd/MM/yyyy Hmm')
Voss
Voss 2024 年 5 月 20 日
Not possible. datetime expects y,m,d or y,m,d,h,m,s, so if you want the h and the m you have to also give the s. Note that the Format doesn't show the seconds anyway.
shamal
shamal 2024 年 5 月 20 日
ok..
Stephen23
Stephen23 2024 年 5 月 20 日
Note that by supplying the units separately you could minimize the seconds to one single 0 and write less code:
A = load('matlab_A.mat').A
A = 1000x4
2008 1 2 501 2008 1 2 502 2008 1 2 503 2008 1 2 504 2008 1 2 505 2008 1 2 506 2008 1 2 507 2008 1 2 508 2008 1 2 509 2008 1 2 510
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
hh = fix(A(:,4)/100);
mm = mod(A(:,4),100);
D = datetime(A(:,1),A(:,2),A(:,3),hh,mm,0, 'Format','dd/MM/yyyy Hmm')
D = 1000x1 datetime array
02/01/2008 501 02/01/2008 502 02/01/2008 503 02/01/2008 504 02/01/2008 505 02/01/2008 506 02/01/2008 507 02/01/2008 508 02/01/2008 509 02/01/2008 510 02/01/2008 511 02/01/2008 512 02/01/2008 513 02/01/2008 514 02/01/2008 515 02/01/2008 516 02/01/2008 517 02/01/2008 518 02/01/2008 519 02/01/2008 520 02/01/2008 521 02/01/2008 522 02/01/2008 523 02/01/2008 524 02/01/2008 525 02/01/2008 526 02/01/2008 527 02/01/2008 528 02/01/2008 529 02/01/2008 530

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

その他の回答 (2 件)

Cris LaPierre
Cris LaPierre 2024 年 5 月 13 日
編集済み: Cris LaPierre 2024 年 5 月 13 日

0 投票

You have the correct function, just the wrong syntax. The biggest issue I see with the conversion is that your time appears to be in millitary format, or HHmm.
I therefore think the simplest approach is to convert your array into a string arrary and then use the syntax t = datetime(DateStrings)
load matlab_A.mat
A
A = 1000x4
2008 1 2 501 2008 1 2 502 2008 1 2 503 2008 1 2 504 2008 1 2 505 2008 1 2 506 2008 1 2 507 2008 1 2 508 2008 1 2 509 2008 1 2 510
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Capture the time
D = datetime(num2str(A),'InputFormat',"yyyy M d Hmm",'Format',"dd/MM/yyyy Hmm")
D = 1000x1 datetime array
02/01/2008 501 02/01/2008 502 02/01/2008 503 02/01/2008 504 02/01/2008 505 02/01/2008 506 02/01/2008 507 02/01/2008 508 02/01/2008 509 02/01/2008 510 02/01/2008 511 02/01/2008 512 02/01/2008 513 02/01/2008 514 02/01/2008 515 02/01/2008 516 02/01/2008 517 02/01/2008 518 02/01/2008 519 02/01/2008 520 02/01/2008 521 02/01/2008 522 02/01/2008 523 02/01/2008 524 02/01/2008 525 02/01/2008 526 02/01/2008 527 02/01/2008 528 02/01/2008 529 02/01/2008 530
shamal
shamal 2024 年 5 月 13 日
編集済み: shamal 2024 年 5 月 13 日

0 投票

thank for answer
The originally array is
it's a very large array
i try your solution but the PC no longer responded
i use ctr+c to break loop..
i try it:
tic
[A,~]=importdata(bubu);
toc
Elapsed time is 3.253890 seconds.

カテゴリ

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

質問済み:

2024 年 5 月 13 日

コメント済み:

2024 年 5 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by