R中从POSIXct到Date的日期转换 [英] Date conversion from POSIXct to Date in R
本文介绍了R中从POSIXct到Date的日期转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能告诉我为什么 R 在下面给出这样的结果:
can anyone tell me why R give such outcome below:
> as.POSIXct("2013-01-01 08:00")
[1] "2013-01-01 08:00:00 HKT"
> as.Date(as.POSIXct("2013-01-01 08:00"))
[1] "2013-01-01"
> as.POSIXct("2013-01-01 07:00")
[1] "2013-01-01 07:00:00 HKT"
> as.Date(as.POSIXct("2013-01-01 07:00"))
[1] "2012-12-31"
2013-01-01
将POSIXct
转换为Date
后不应该是2013-01-01
for 2013-01-01 07:00
,有什么办法可以把截止时间从08:00
改成00:00
?
Shouldn't it be 2013-01-01
after converting POSIXct
to Date
for 2013-01-01 07:00
, is there any way to change the cutoff from 08:00
to 00:00
?
我发现以下内容可以解决我的问题,但方式不太好
I found the following can fix my problem, but in a less neat way
> as.Date(as.character(as.POSIXct("2013-01-01 07:00")))
[1] "2013-01-01"
推荐答案
这里的问题是时区 - 您可以看到您在 "HKT"
中.试试:
The problem here is timezones - you can see you're in "HKT"
. Try:
as.Date(as.POSIXct("2013-01-01 07:00", 'GMT'))
[1] "2013-01-01"
从 ?as.Date()
:
["POSIXct
" is] 通过忽略午夜后的时间转换为天在指定时区的时间表示中,默认UTC
["
POSIXct
" is] converted to days by ignoring the time after midnight in the representation of the time in specified timezone, default UTC
这篇关于R中从POSIXct到Date的日期转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文