Datevec to Datetime conversion results in 1 millisecond error?
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Brad
el 28 de Oct. de 2015
Comentada: Brad
el 28 de Oct. de 2015
When converting a datevector to a datetime, it appears that I'm losing a millisecond and I can't figure out why. It doesn't appear to be a rounding or formatting issue. Any help on what's going on here would be much appreciated.
Thanks,
Brad
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/151124/image.jpeg)
0 comentarios
Respuesta aceptada
Peter Perkins
el 28 de Oct. de 2015
Screen shots are probably not the most convenient way for others to diagnose this problem.
You haven't lost a millisecond, you never had it to begin with. Here's the deal:
>> dv = [2015 8 13 21 6 19.172; 2015 8 13 21 6 19.180]
dv =
2015 8 13 21 6 19.172
2015 8 13 21 6 19.18
You only think that last value is 19.18. It isn't. It's the floating point value that's closest to 19.18, which happens to be just less than the real number 19.18. The display simply shields you from that. With "numbers", that's probably what you'd want display to do, but in "time", you'd probably be unhappy if the display for something just before midnight rounded up to the next day. So:
>> dt = datetime(2015,8,13,21,6,[19.172 19.180],'Format','dd-MM-yyyy HH:mm:ss.SSSSSS')
dt =
13-08-2015 21:06:19.172000 13-08-2015 21:06:19.179999
Your problem is that by the time you've typed "19.18", you're done for. datetime has the precision to give you what you want, though:
>> dt = datetime('13-08-2015 21:06:19.180','Format','dd-MM-yyyy HH:mm:ss.SSSSSS')
dt =
13-08-2015 21:06:19.180000
>> dt = datetime(2015,8,13,21,6,19,[172 180],'Format','dd-MM-yyyy HH:mm:ss.SSSSSS')
dt =
13-08-2015 21:06:19.172000 13-08-2015 21:06:19.180000
Más respuestas (1)
Ingrid
el 28 de Oct. de 2015
I do not seem to be able to reproduce this deviation, so how did you obtain the DV and DT variables?
>> timeVec = [2015 8 13 21 6 19.1800]
timeVec =
1.0e+03 *
Columns 1 through 5
2.015000000000000 0.008000000000000 0.013000000000000 0.021000000000000 0.006000000000000
Column 6
0.019180000000000
>> timeNum = datenum(timeVec)
timeNum =
7.361898793886574e+05
>> timeVecBack = datevec(timeNum)
timeVecBack =
1.0e+03 *
Columns 1 through 4
2.015000000000000 0.008000000000000 0.013000000000000 0.021000000000000
Columns 5 through 6
0.006000000000000 0.019180000000008
1 comentario
Ver también
Categorías
Más información sobre Calendar en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!