필터 지우기
필터 지우기

Epoch time conversion using datetime function

조회 수: 64 (최근 30일)
Aditya
Aditya 2022년 7월 5일
답변: Karim 2022년 7월 5일
Hello i am trying to convert epoch time to date time using following command
datetime(1653128376288, 'convertfrom', 'posixtime', 'Format', 'MM/dd/yyyy HH:mm:ss.SSS','TimeZone','Europe/Zurich') and
the answer comes to be 07/22/54355 08:44:48.000 which is obviously wrong,
however if i use an online tool (see below) than it gives me the correct answer. Can anyone help me to find what wrong? i need to convert an array of 30000 rows. thanx

채택된 답변

Stephen23
Stephen23 2022년 7월 5일
편집: Stephen23 2022년 7월 5일
Your value is in milliseconds (not seconds, as is standard for Unix time), so either divide the value by 1000:
opt = {'Format', 'eeee dd MM yyyy HH:mm:ss.SSS'};
datetime(1650000000000/1000, opt{:}, 'convertfrom','posixtime')
ans = datetime
Friday 15 04 2022 05:20:00.000
or specify the 'tickspersecond' option:
datetime(1650000000000, opt{:}, 'convertfrom', 'epochtime','Epoch','1-Jan-1970','TicksPerSecond',1000)
ans = datetime
Friday 15 04 2022 05:20:00.000

추가 답변 (1개)

Karim
Karim 2022년 7월 5일
One approach would be to add the "ticks per second" parameter, in your case set it to 1000:
EpochVal = 1650000000000;
% convert epoch time
MyTime = datetime(EpochVal,'ConvertFrom','epochtime','Epoch','1970-01-01','TicksPerSecond',1000)
MyTime = datetime
15-Apr-2022 05:20:00
% convert to the required format
MyFormattedTime = datetime(MyTime,'Format','MM/dd/yyyy HH:mm:ss.SSS','TimeZone','Europe/Zurich')
MyFormattedTime = datetime
04/15/2022 05:20:00.000

카테고리

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