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"`
In the Format Cells window, go to the Number tab, select Custom from the Category list, and enter h:mm:ss. 000 in the Type text box. As a result, all of the time values are displayed with milliseconds as decimals.
Approach : First declare variable time and store the milliseconds of current date using new date() for current date and getTime() Method for return it in milliseconds since 1 January 1970. Convert time into date object and store it into new variable date. Convert the date object's contents into a string using date.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With