# how to convert hours and minutes from decimals to datetime objects

조회 수: 19 (최근 30일)
Salma fathi 2022년 12월 19일
댓글: Salma fathi 2022년 12월 21일
having as the follwing two columns from a table, the first column represents the year-month-day and the second column represent hours and minutes in decimal based on the equation (hour+min/60), how can I mrge the two columns to get at the end a full datetim object with the format
yyyy-MM-dd hh-mm?

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

### 채택된 답변

Peter Perkins 2022년 12월 19일
Converting to string is totally unnecessary. Assuming the first variable in the table is already a datetime:
>> t = datetime(2008,05,07);
>> t + hours(15.9)
ans =
datetime
07-May-2008 15:54:00
If the first var is not a datetime, figure out why, and make it one. And probably make the table a timetable.
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Salma fathi 2022년 12월 21일
Thank you, it worked perfectly.

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

### 추가 답변 (1개)

prasanth s 2022년 12월 19일
First convert it into string type with the format "yyyy-MM-dd hh-mm" using
hours_decimal=15.9
myDate=string("2008-05-07 ")+string(floor(hours_decimal))+"-"+string(60*(hours_decimal-floor(hours_decimal)))
convert to datetime object using using
t = datetime(myDate,'InputFormat','yyyy-MM-dd HH-mm')

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

### 카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by