How to convert time stamp string “2014-07-20T05:11:49.988Z” into POSIXt in R?

前端 未结 2 1668
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 20:43

How to convert time stamp string “2014-07-20T05:11:49.988Z” into POSIXt in R?

I want to know why the second is represented in 3 decimel places? also what is the mean

相关标签:
2条回答
  • 2020-12-10 21:05

    The lubridate package has very robust parsers that I tend to prefer over manual specification of the format in base R

    library(lubridate)
    #> 
    #> Attaching package: 'lubridate'
    #> The following object is masked from 'package:base':
    #> 
    #>     date
    
    (t <- ymd_hms("2014-07-20T05:11:49.998Z"))
    #> [1] "2014-07-20 05:11:49 UTC"
    

    Created on 2019-03-11 by the reprex package (v0.2.1)

    Making sure that the milliseconds that aren't printed aren't lost either:

    second(t)
    #> [1] 49.998
    
    0 讨论(0)
  • 2020-12-10 21:26

    The "Z" is shorthand for UTC. You can parse this in base R with

    x <- as.POSIXct("2014-07-20T05:11:49.998Z", 
        format="%Y-%m-%dT%H:%M:%OSZ", tz="GMT")
    

    Note that you generally either use POSIXct or POSIXlt rather than POSIXt directly (both have POSIXt as a base class)

    0 讨论(0)
提交回复
热议问题