Convert Unix Time in Date Time Format with Milliseconds

조회 수: 54 (최근 30일)
Sarah Maag
Sarah Maag 2019년 2월 6일
댓글: Peter Perkins 2024년 11월 25일
Hello,
i want to convert Unix Time Stamp like this 1545390864126080000 (1.545^18) in a Format like this "Friday, 21. December 2018 11:14:24.126".
I use:
date_time = datestr(unix_time_pose./86400 + datenum(1970,1,1));
unix_time_pose vector is Unix_Time/10^9 and i get something like this '21-Dec-2018 12:04:58'. It is the right date without milliseconds.
Can anyone help me how to get milliseconds,too?
  댓글 수: 2
James Tursa
James Tursa 2019년 2월 6일
편집: James Tursa 2019년 2월 6일
What does this show:
whos unix_time_pose
Sarah Maag
Sarah Maag 2019년 2월 7일
I have a mobile robot. I get the pose from the odometry of the robot. i only named the vector with the unix time stamps unix_time_pose. Each entry of the vector unix_time_pose has the format unix_time/10⁹ (like 1.545^9) because I divided 1.545^18/10⁹.

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

채택된 답변

Peter Perkins
Peter Perkins 2019년 2월 7일
What you have is 1ns ticks since 1970. That's sort of Posix time, but at a different resolution. If that's really what you have, you are gonna need to store the raw numbers as a uint64 array, double will not give you enough precision. So
>> t = uint64(1545390864126080000)
t =
uint64
1545390864126080000
>> d = datetime(t,'ConvertFrom','epochtime','TicksPerSecond',1e9,'Format','dd-MMM-yyyy HH:mm:ss.SSSSSSSSS')
d =
datetime
21-Dec-2018 11:14:24.126080000
If you don't care about anything smaller than ms, then you can use double.
>> t = 1545390864126
t =
1545390864126
>> d = datetime(t,'ConvertFrom','epochtime','TicksPerSecond',1e3,'Format','dd-MMM-yyyy HH:mm:ss.SSS')
d =
datetime
21-Dec-2018 11:14:24.126
  댓글 수: 5
Sarah Maag
Sarah Maag 2019년 2월 10일
I updated my Software and installed more packages. Now it works. Thanks a lot.
Taichi Kurayama
Taichi Kurayama 2021년 4월 20일
Thank you so much!

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

추가 답변 (1개)

eldar
eldar 2024년 9월 11일
If you are reading data in unix timestamp from a .CSV file with a floating point as milliseconds what worked for me was,
dt = datetime(1970,1,1,'Format','dd-MMM-yyyy HH:mm:ss.SSS') + tableData.UnixTime/86400;
  댓글 수: 3
eldar
eldar 2024년 11월 24일
Hi Peter, I tried that at first but for some reason did not get the precession that was needed.
Peter Perkins
Peter Perkins 2024년 11월 25일
I don't know what that means, but surely if you are converting from seconds to days (using UnixTime/86400) you will be introducing unnecessary noise into your calculations, even if very small.

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

카테고리

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