简体   繁体   English

如何在R中将时间戳字符串“ 2014-07-20T05:11:49.988Z”转换为POSIXt?

[英]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? 如何在R中将时间戳字符串“ 2014-07-20T05:11:49.988Z”转换为POSIXt?

I want to know why the second is represented in 3 decimel places? 我想知道为什么第二个代表3个分位数? also what is the meaning of appending the 'Z' at the end of time stamp? 在时间戳的末尾附加“ Z”又是什么意思? Can anybody know how this string can be converted into time in R 任何人都可以知道如何将字符串转换为R中的时间

The "Z" is shorthand for UTC . “ Z”是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) 请注意,通常您通常使用POSIXct或POSIXlt而不是直接使用POSIXt(两者都将POSIXt作为基类)

The lubridate package has very robust parsers that I tend to prefer over manual specification of the format in base R lubridate软件包具有非常强大的解析器,我倾向于在基数R中首选手动formatformat

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) reprex软件包 (v0.2.1)创建于2019-03-11

Making sure that the milliseconds that aren't printed aren't lost either: 确保未打印的毫秒数也不会丢失:

second(t)
#> [1] 49.998

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字符串“2019-10-09T06:07:05.888Z”转换为日历日期和时间 - How to convert string “2019-10-09T06:07:05.888Z” as calendar date and time 如何将时间戳 07 Mar 2016 01:00:03 PM 转换为 r 中的数字 - how to convert time stamp 07 Mar 2016 01:00:03 PM to numeric in r 在R中,如何将类似“ 2013年10月5日星期六20:31:59”的字符串转换为“ 2013-10-05星期六20:31:59”? - In R, how to convert a string like “Saturday, 5 Oct 2013 20:31:59” to “2013-10-05 Saturday 20:31:59”? 将 POSIXt 时间转换为儒略日 - Convert A POSIXt Time To A Julian Day R:如何将字符串数据帧转换为 POSIXt 对象? - R: How do I convert a dataframe of strings into POSIXt objects? 如何使用 R 将 ISO Timestamp 数据列转换为 POSIXt? - How to use R to convert column of ISO Timestamp data into POSIXt? 如何将包含 T 和 z 的时间戳(例如 - 2020-03-02T16:30:36Z)转换为 dd/mm/yyy hh:mm:ss - How to convert time stamp containing T and z ( for ex - 2020-03-02T16:30:36Z) to dd/mm/yyy hh:mm:ss 如何使用 R 将数字转换为时间戳 - How to convert numeric numbers to time stamp using R 如何从R中的字符串中分割和分隔时间戳 - How to split and separate time stamp from a string in R 将spark数据框中的字符串转换为时间戳 - Convert string in spark data frame to time stamp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM