conversion of date from yyyymmddHHMMSS format to yymmddHHMMSS format.

Hi all can anyone please help with the conversion of date from yyyymmddHHMMSS format to yymmddHHMMSS format.
for example I want to change column 1 in the following dataset: (e.g row 1 shoud be 200723000000)
'2020,07,23,00,00,00' -0.800000000000000
'2020,07,23,00,00,10' -0.800000000000000
'2020,07,23,00,00,20' -0.700000000000000
'2020,07,23,00,00,30' -0.700000000000000
'2020,07,23,00,00,40' -0.700000000000000
'2020,07,23,00,00,50' -0.800000000000000
'2020,07,23,00,01,00' -0.700000000000000
'2020,07,23,00,01,10' -0.700000000000000
'2020,07,23,00,01,20' -0.700000000000000
'2020,07,23,00,01,30' -0.600000000000000
'2020,07,23,00,01,40' -0.600000000000000
'2020,07,23,00,01,50' -0.600000000000000
'2020,07,23,00,02,00' -0.500000000000000
'2020,07,23,00,02,10' -0.500000000000000
'2020,07,23,00,02,20' -0.500000000000000
'2020,07,23,00,02,30' -0.600000000000000
'2020,07,23,00,02,40' -0.500000000000000
'2020,07,23,00,02,50' -0.500000000000000
'2020,07,23,00,03,00' -0.500000000000000
'2020,07,23,00,03,10' -0.500000000000000
'2020,07,23,00,03,20' -0.500000000000000

Risposte (1)

vec = datevec( '2020,07,23,00,00,00', 'yyyy,mm,dd,HH,MM,SS' );
datestr( vec, 'yymmddHHMMSS' )
ans = '200723000000'
or did you mean
datestr( vec, 'yy,mm,dd,HH,MM,SS' )
ans = '20,07,23,00,00,00'

7 Commenti

Thank you , It worked, I meant the first one
Hi per, I am trying the following code for me now to veiw the datevector and I am having an error as shown below. i have attached sample dataset im reading from a txt file.
t = readtable('E:\Data\2020\B220_DT80\20200724T000000.txt');
t(1:8642,:);
height(t);
rng('default')
dates = t.Var1;
durations = t.Var2;
T = table(dates, durations);
fullDateTime = dateshift(T.dates,'start','day') + T.durations;
fullDateTime.Format = 'yyyyMMddHHmmSS';
T.DateTime = datestr(fullDateTime,'yymmddHHMMSS');
T.DNI = t.Var4;
refdata = T(:,{'DateTime','DNI'});
timestampVec1 = datenum(datevec(timestampVec1))
Error using datevec (line 282)
Cannot parse date 200723000000.
Error in test2 (line 18)
timestampVec1 = datenum(datevec(timestampVec1)
hi Per,
so I ran the following code but my date vector is not correct, please see the following:
t = readtable('E:\Data\2020\B220_DT80\20200724T000000.txt');
t(1:8642,:);
height(t);
rng('default')
dates = t.Var1;
durations = t.Var2;
T = table(dates, durations);
fullDateTime = dateshift(T.dates,'start','day') + T.durations;
fullDateTime.Format = 'MM/dd/yyyy HH:mm:ss';
T.DateTime = fullDateTime;
T.DNI = t.Var4;
refdata = T(:,{'DateTime','DNI'});
timestampVec1 = datenum(datevec(T.DateTime))
and when i ran,
datevec(T.DateTime), the following came up it looks like its adding zeros, insted of (2020 07 23 08 52 20), its giving(2.0200 0.0070 0.0230 0.0080 0.0520 0.0202).
2.0200 0.0070 0.0230 0.0080 0.0520 0.0202
2.0200 0.0070 0.0230 0.0080 0.0520 0.0302
2.0200 0.0070 0.0230 0.0080 0.0520 0.0402
2.0200 0.0070 0.0230 0.0080 0.0520 0.0502
2.0200 0.0070 0.0230 0.0080 0.0530 0.0002
2.0200 0.0070 0.0230 0.0080 0.0530 0.0102
2.0200 0.0070 0.0230 0.0080 0.0530 0.0202
2.0200 0.0070 0.0230 0.0080 0.0530 0.0302
2.0200 0.0070 0.0230 0.0080 0.0530 0.0402
2.0200 0.0070 0.0230 0.0080 0.0530 0.0502
2.0200 0.0070 0.0230 0.0080 0.0540 0.0002
2.0200 0.0070 0.0230 0.0080 0.0540 0.0102
2.0200 0.0070 0.0230 0.0080 0.0540 0.0202
2.0200 0.0070 0.0230 0.0080 0.0540 0.0302
2.0200 0.0070 0.0230 0.0080 0.0540 0.0402
2.0200 0.0070 0.0230 0.0080 0.0540 0.0502
2.0200 0.0070 0.0230 0.0080 0.0550 0.0002
2.0200 0.0070 0.0230 0.0080 0.0550 0.0102
2.0200 0.0070 0.0230 0.0080 0.0550 0.0202
2.0200 0.0070 0.0230 0.0080 0.0550 0.0302
2.0200 0.0070 0.0230 0.0080 0.0550 0.0402
2.0200 0.0070 0.0230 0.0080 0.0550 0.0502
2.0200 0.0070 0.0230 0.0080 0.0560 0.0002
2.0200 0.0070 0.0230 0.0080 0.0560 0.0102
In this case you need to provide a format string. datevec() isn't needed
timestampVec1 = '200723000000';
timestampNum1 = datenum(datevec(timestampVec1,'yymmddHHMMSS'))
timestampNum1 = 737995
timestampNum2 = datenum(timestampVec1,'yymmddHHMMSS')
timestampNum2 = 737995
timestampVec1 is not defined in your script. The last row has little to do with the rest of your script. However,
timestampNum = datenum(datevec(timestampVec1));
Error using datevec (line 289)
Cannot parse date 200723000000.
reproduces the error you report.
hi Per,
so I ran the following code but my date vector is not correct, please see the following:
t = readtable('E:\Data\2020\B220_DT80\20200724T000000.txt');
t(1:8642,:);
height(t);
rng('default')
dates = t.Var1;
durations = t.Var2;
T = table(dates, durations);
fullDateTime = dateshift(T.dates,'start','day') + T.durations;
fullDateTime.Format = 'MM/dd/yyyy HH:mm:ss';
T.DateTime = fullDateTime;
T.DNI = t.Var4;
refdata = T(:,{'DateTime','DNI'});
timestampVec1 = datenum(datevec(T.DateTime))
and when i ran,
datevec(T.DateTime)
the following came up it looks like its adding zeros, insted of (2020 07 23 08 52 20), its giving(2.0200 0.0070 0.0230 0.0080 0.0520 0.0202).
2.0200 0.0070 0.0230 0.0080 0.0530 0.0002
2.0200 0.0070 0.0230 0.0080 0.0530 0.0102
2.0200 0.0070 0.0230 0.0080 0.0530 0.0202
2.0200 0.0070 0.0230 0.0080 0.0530 0.0302
2.0200 0.0070 0.0230 0.0080 0.0530 0.0402
2.0200 0.0070 0.0230 0.0080 0.0530 0.0502
2.0200 0.0070 0.0230 0.0080 0.0540 0.0002
2.0200 0.0070 0.0230 0.0080 0.0540 0.0102
2.0200 0.0070 0.0230 0.0080 0.0540 0.0202
2.0200 0.0070 0.0230 0.0080 0.0540 0.0302
2.0200 0.0070 0.0230 0.0080 0.0540 0.0402
2.0200 0.0070 0.0230 0.0080 0.0540 0.0502
2.0200 0.0070 0.0230 0.0080 0.0550 0.0002
2.0200 0.0070 0.0230 0.0080 0.0550 0.0102
2.0200 0.0070 0.0230 0.0080 0.0550 0.0202
2.0200 0.0070 0.0230 0.0080 0.0550 0.0302
2.0200 0.0070 0.0230 0.0080 0.0550 0.0402
2.0200 0.0070 0.0230 0.0080 0.0550 0.0502
2.0200 0.0070 0.0230 0.0080 0.0560 0.0002
2.0200 0.0070 0.0230 0.0080 0.0560 0.0102
First a couple of comments
  • format your question and comments so that they are easier to read an copy
  • keep test cases small and to the point, e.g. there is no need for a large text file
  • read answers and comments carefully and tell if you don't understand
I added a format string (I'm om R2018b)
%%
t = readtable('20200724T000000.txt');
dates = t.Var1;
durations = t.Var2;
T = table(dates, durations);
fullDateTime = dateshift(T.dates,'start','day') + T.durations;
fullDateTime.Format = 'yyyyMMddHHmmSS';
T.DateTime = datestr(fullDateTime,'yymmddHHMMSS');
%%
datevec(T.DateTime(1:2,:),'yymmddHHMMSS')
ans = 2×6
2020 7 23 0 0 0 2020 7 23 0 0 10
The value of T.DateTime is a character array, e.g.
T.DateTime(1:2,:)
ans = 2×12 char array
'200723000000' '200723000010'
Wasn't there a string like 1.0e+4 above the output
format short
magic(4).*1e-6
ans = 4×4
1.0e+-4 * 0.1600 0.0200 0.0300 0.1300 0.0500 0.1100 0.1000 0.0800 0.0900 0.0700 0.0600 0.1200 0.0400 0.1400 0.1500 0.0100
I fail to reproduce your output
datevec(T.DateTime(1:2,:))
Error using datevec (line 289)
Cannot parse date 200723000000.
Thank you so much Per, i needed to pass it in this format (datevec(T.DateTime(1:2,:),'yymmddHHMMSS')) and it worked!

Accedi per commentare.

Richiesto:

il 13 Mag 2021

Modificato:

il 13 Mag 2021

Community Treasure Hunt

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

Start Hunting!

Translated by