How can I convert runtime into datetime?

조회 수: 11 (최근 30일)
Dan
Dan 2020년 6월 11일
편집: Steven Lord 2020년 6월 12일
I have a this type of run time data
142369240.800000
142369240.800000
142369241
142369241.200000
142369241.200000
I am using the below datetime option to convert into d-MMM-y HH:mm:ss.sss, but getting results in CE. Please can you help us out? thanks
t = datetime(142369240.800000,'ConvertFrom','datenum','Format','d-MMM-y HH:mm:ss.sss')
t =
datetime
389793 CE

채택된 답변

Steven Lord
Steven Lord 2020년 6월 11일
There's a Note in the description of the Format property on the documentation page for datetime that states "Datetime values later than 144683 years CE or before 140743 BCE display only the year numbers, regardless of the specified Format value."
You could break this up into year, month, and day values if your value actually represents a date.
>> dt1 = datetime(142369240.800000, 'ConvertFrom' ,'datenum');
>> [y, m, d] = ymd(dt1)
y =
389793
m =
9
d =
26
But since you mentioned runtime, I'm guessing your number doesn't represent a date. Does it represent a number of seconds, milliseconds, microseconds, or the like? If so you probably don't want to create a datetime but instead want a duration.
duSeconds = seconds(142369240.8)
years(duSeconds) % about four and a half years
  댓글 수: 2
Dan
Dan 2020년 6월 12일
run time is the machine run time and is in seconds. I actually wanted it in dd-MM-yyyy HH:mm:ss.SSS format. But I think that's not possible. So I found another variable. Its a machine's time stamp in char array. Here is an example of first few lines
'14:53:20.203'
'14:53:20.218'
'14:53:20.328'
'14:53:20.437'
'14:53:20.531'
Since it doesn't had a date, I extracted the date from the filename. Here is an example
date =
'02-22-2017'
and then used strcast command to add date to the time stamp.
d = strcat(date,{' '},timestamp);
>> d (1:5, :)
ans =
5×1 cell array
{'02-22-2017 14:53:20.203'}
{'02-22-2017 14:53:20.218'}
{'02-22-2017 14:53:20.328'}
{'02-22-2017 14:53:20.437'}
{'02-22-2017 14:53:20.531'}
But I cannot still use this variable to plot other variables against time, so I used datetime function to convert it and got the below parse error.
t = datetime(d,'InputFormat','dd-MM-yyyy HH:mm:ss.SSS', 'Format', 'yyyy-MM-dd HH:mm:ss.SSS');
Error using datetime (line 616)
Unable to parse date/time text using the format 'dd-MM-yyyy HH:mm:ss.SSS'.
I even tried converting the cell array 'd' to char array d2 = char(d); and then used datetime again, but still I got the error.
t = datetime(d2,'InputFormat','dd-MM-yyyy HH:mm:ss.SSS', 'Format', 'yyyy-MM-dd HH:mm:ss.SSS');
Error using datetime (line 616)
Unable to parse date/time text using the format 'dd-MM-yyyy HH:mm:ss.SSS'.
Not sure what's causing the error, is it datetime cannot read the date backward or we cannot add a date to timestamp using strcat.
Steven Lord
Steven Lord 2020년 6월 12일
편집: Steven Lord 2020년 6월 12일
run time is the machine run time and is in seconds. I actually wanted it in dd-MM-yyyy HH:mm:ss.SSS format.
Do you know when the machine started running? Trying to convert say 10 seconds to a date and time doesn't work, but trying to convert 10 seconds from right now does.
s = seconds(10);
tenSecondsFromNow = datetime('now')+s % works
tenSecondsAsDatetime = datetime(s) % does not work
That's like the person who calls up a business and asks "How far away are you?" Without knowing where the caller is, that question is unanswerable (except in general terms, like "Less than a million miles.")
But I cannot still use this variable to plot other variables against time, so I used datetime function to convert it and got the below parse error.
t = datetime(d,'InputFormat','dd-MM-yyyy HH:mm:ss.SSS', 'Format', 'yyyy-MM-dd HH:mm:ss.SSS');
That's correct. d starts off '02-22-2017'. What's the 22nd month of the year? Try this, with the 'dd' and 'MM' parts of the InputFormat swapped:
t = datetime(d,'InputFormat','MM-dd-yyyy HH:mm:ss.SSS', ...
'Format', 'yyyy-MM-dd HH:mm:ss.SSS');

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

추가 답변 (0개)

카테고리

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