how to create array datetime

1 view (last 30 days)
Luca Re
Luca Re on 13 May 2024
Commented: Stephen23 on 20 May 2024
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 Comment
Cris LaPierre
Cris LaPierre on 13 May 2024
Based on your graphic, 01/02/2008 501 is actually (month/day/year time)

Sign in to comment.

Accepted Answer

Voss
Voss on 13 May 2024
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 Comments
Stephen23
Stephen23 on 20 May 2024
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

Sign in to comment.

More Answers (2)

Cris LaPierre
Cris LaPierre on 13 May 2024
Edited: Cris LaPierre on 13 May 2024
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

Luca Re
Luca Re on 13 May 2024
Edited: Luca Re on 13 May 2024
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.

Community Treasure Hunt

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

Start Hunting!