将日期转换为POSIXct [英] Convert Date to POSIXct

查看:128
本文介绍了将日期转换为POSIXct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下列日期在转换为POSIXct时变为2014-07-07?

  Sys.setenv TZ ='America / Sao_Paulo')
d< - as.Date(2014-07-08,format =%Y-%m-%d)
d
[1 ]2014-07-08
as.POSIXct(d)
[1]2014-07-07 21:00:00 BRT
/ pre>

解决方案

因为 as.POSIXct.Date 对于时区(如果您在 ... 中指定)和日期,则不会将其传递给 .POSIXct 对象是UTC,所以您的 POSIXct 与Date对象的UTC偏移。



更好地直接在字符串上调用 as.POSIXct ,如果可以:

 > as.POSIXct(2014-07-08,format =%Y-%m-%d)
[1]2014-07-08 BRT


Why does the Date below change to "2014-07-07" when converted to POSIXct?

Sys.setenv(TZ='America/Sao_Paulo')
d <- as.Date("2014-07-08", format="%Y-%m-%d")
d
[1] "2014-07-08"
as.POSIXct(d)
[1] "2014-07-07 21:00:00 BRT"

解决方案

Because as.POSIXct.Date doesn't look for a timezone (and won't pass it to .POSIXct if you specify it in ...) and Date objects are "UTC", so your POSIXct is offset from the UTC of the Date object.

It would be better to call as.POSIXct on the character string directly, if you can:

> as.POSIXct("2014-07-08", format="%Y-%m-%d")
[1] "2014-07-08 BRT"

这篇关于将日期转换为POSIXct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆