简体   繁体   中英

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

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 meaning of appending the 'Z' at the end of time stamp? Can anybody know how this string can be converted into time in R

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM