How to parse milliseconds?

RDatetimeTime SeriesStrptime

R Problem Overview


How do I use strptime or any other functions to parse time stamps with milliseconds in R?

time[1]
# [1] "2010-01-15 13:55:23.975"
strptime(time[1], format="%Y-%m-%d %H:%M:%S.%f")
# [1] NA
strptime(time[1], format="%Y-%m-%d %H:%M:%S")
# [1] "2010-01-15 13:55:23"`

R Solutions


Solution 1 - R

Courtesy of the ?strptime help file (with the example changed to your value):

 z <- strptime("2010-01-15 13:55:23.975", "%Y-%m-%d %H:%M:%OS")
 z # prints without fractional seconds
 op <- options(digits.secs=3)
 z
 options(op) #reset options

Solution 2 - R

You can also use strptime(time[1], "%OSn") where 0 <= n <= 6, without having to set digits.secs.

The documentation states "Which of these are supported is OS-dependent." so YMMV.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsignalseekerView Question on Stackoverflow
Solution 1 - RAnikoView Answer on Stackoverflow
Solution 2 - Ruser244343View Answer on Stackoverflow