conversion of date from yyyymmddHHMMSS format to yymmddHHMMSS format.

조회 수: 23 (최근 30일)
Remember Samu
Remember Samu 2021년 5월 13일
편집: per isakson 2021년 5월 13일
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

답변 (1개)

per isakson
per isakson 2021년 5월 13일
편집: per isakson 2021년 5월 13일
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
per isakson
per isakson 2021년 5월 13일
편집: per isakson 2021년 5월 13일
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.
Remember Samu
Remember Samu 2021년 5월 13일
Thank you so much Per, i needed to pass it in this format (datevec(T.DateTime(1:2,:),'yymmddHHMMSS')) and it worked!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by