Turn number to datetime
古いコメントを表示
Is there an easy way to convert a number, e.g. 101416517, to a time: 10:14:16.517?
Thanks in advance!
採用された回答
その他の回答 (4 件)
Cris LaPierre
2021 年 5 月 16 日
編集済み: Cris LaPierre
2021 年 5 月 16 日
d=101416517;
D=datetime(num2str(d),'InputFormat','HHmmssSSS',"Format","HH:mm:ss.SSS")
2 件のコメント
Dylan den Hartog
2021 年 5 月 16 日
dpb makes a good point that a duration may be more appropriate if you truly just want the time. Here is an approach to get that:
d=101416517;
D=datetime(num2str(d),'InputFormat','HHmmssSSS')
D = D-dateshift(D,"start","day");
D.Format="hh:mm:ss.SSS"
dpb
2021 年 5 月 16 日
>> dtn=datetime(string(tn),'InputFormat','HHmmssSSS','Format','HHmmss.SSS')
dtn =
datetime
101416.517
>>
NB: However, a MATLAB datetime must include a date; in the above without an input date field, the current date at the local time of the conversion will be used -- for example, carrying on from above
>> dtn.Format='default'
dtn =
datetime
16-May-2021 10:14:16
>>
To hold only the time, one would have to use a duration variable, but, unfortunately, duration() is a weak sibling --
>> duration(string(tn),'InputFormat','HHmmssSSS','Format','hh:mm:ss.SSS')
Error using duration (line 299)
Unsupported format 'HHmmssSSS'. See the documentation of 'InputFormat' for valid formats.
>>
The general input format strings aren't recognized; nor can you be fully general in the output format requested. Real slip-up in quality of implementation arena.
So, you can coerce in round about way by subtracting the y,m,d from the datetime value...
>> dun=duration(dtn-datetime(year(dtn),month(dtn),day(dtn)),'Format','hh:mm:ss.SSS')
dun =
duration
10:14:16.517
>>
Alternatively, you can format the string in a recognizable pattern...with some effort
>> stn=num2str(tn);
>> sprintf('%2s:%2s:%2s.%3s',stn(1:2),stn(3:4),stn(5:6),stn(7:9))
ans =
'10:14:16.517'
>>
Or, you can do all the arithmetic to modulo the pieces by the proper powers of 10 to numerically separate out the pieces...
Dylan den Hartog
2021 年 5 月 16 日
0 投票
3 件のコメント
Yes. Add the date to the duration, or just prepend the date to times (example below)
start_date_number = 03042021;
time_numbers = [101416488; 101416490; 101416517; 101416546; 101416548];
string(start_date_number)+string(time_numbers)
% I use sprintf to preserve the leading zero
D = datetime(sprintf('%08d',start_date_number)+string(time_numbers),'InputFormat',"MMddyyyyHHmmssSSS",'Format','MM-dd-yyyy HH:mm:ss.SSS')
Dylan den Hartog
2021 年 5 月 16 日
Cris LaPierre
2021 年 5 月 16 日
Yes, if your times reset to 0 each day, this will be a problem. What data do you have to work with? Could you share a data set? You can attach it using the paperclip icon.
Dylan den Hartog
2021 年 5 月 16 日
0 投票
1 件のコメント
Let's take a series of datetime values.
N = datetime('now')
dur = hours(randi([0 5], 10, 1)) + ... % Between 0 and 5 hours from now
minutes(randi([0 59], 10, 1)) + ... % and 0 to 59 minutes
seconds(randi([0 59], 10, 1)); % and 0 to 59 seconds
dur.Format = 'hh:mm:ss';
dur = sort(dur)
DT = N+dur
Given N and the datetime array DT we can recreate dur.
offset = DT - N
And if we want to figure out what would happen if we had started this count from say 4 AM today instead of from N:
newbase = datetime(2021, 5, 16, 4, 0, 0)
newDT = newbase + offset
The times in newDT should be about 14.5 hours before the times in DT.
difference = DT - newDT
differenceInHours = hours(difference)
In general:
- datetime + datetime gives an error (rather than a play between the pitcher and catcher.)
- datetime - datetime gives a duration
- datetime + duration gives a datetime
- datetime - duration gives a datetime
- duration + datetime gives a datetime
- duration - datetime gives an error
- duration + duration gives a duration
- duration - duration gives a duration
カテゴリ
ヘルプ センター および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!