Convert Unix Time in Date Time Format with Milliseconds
147 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sarah Maag
le 6 Fév 2019
Commenté : Peter Perkins
le 21 Nov 2024 à 20:28
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 commentaires
James Tursa
le 6 Fév 2019
Modifié(e) : James Tursa
le 6 Fév 2019
What does this show:
whos unix_time_pose
Réponse acceptée
Peter Perkins
le 7 Fév 2019
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 commentaires
Plus de réponses (1)
eldar mamedov
le 11 Sep 2024
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;
1 commentaire
Peter Perkins
le 21 Nov 2024 à 20:28
Eldar, this works, but you will likely be happier using the much simpler
datetime(unixTime,"ConvertFrom","posixtime")
Voir également
Catégories
En savoir plus sur Dates and Time dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!