How to parse milliseconds?

49,196

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
Share:
49,196
signalseeker
Author by

signalseeker

data scientist + hacker + quant trader

Updated on December 05, 2020

Comments

  • signalseeker
    signalseeker over 3 years

    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"`
    
  • signalseeker
    signalseeker over 14 years
    Thanks, I missed that in the strptime doc. I was looking for a format character and gave up when I did not see one.
  • jkff
    jkff over 13 years
    If I could put a memorial in your honor, I would!
  • Pierre D
    Pierre D over 11 years
    so would I! The "%OS" bit is awesome.
  • firebush
    firebush over 8 years
    Is this only in python3 or something? In my python2.7.8: >>> time.strptime(t, "%Y-%m-%d %H:%M:%OS") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/pythons/2.7.8/lib/python2.7/_strptime.py", line 467, in _strptime_time return _strptime(data_string, format)[0] File "/opt/pythons/2.7.8/lib/python2.7/_strptime.py", line 317, in _strptime (bad_directive, format)) ValueError: 'O' is a bad directive in format '%Y-%m-%d %H:%M:%OS'
  • IRTFM
    IRTFM about 8 years
    @firebush: It's in R. It might require "%Y-%m-%d %H:%M:%OS3" on some platforms. The implementation of the "OS" format is labeled as OS-specific on the help pages.