How can I convert runtime into datetime?

12 visualizzazioni (ultimi 30 giorni)
Dan
Dan il 11 Giu 2020
Modificato: Steven Lord il 12 Giu 2020
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

Risposta accettata

Steven Lord
Steven Lord il 11 Giu 2020
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 Commenti
Dan
Dan il 12 Giu 2020
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 il 12 Giu 2020
Modificato: Steven Lord il 12 Giu 2020
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');

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by