简体   繁体   English

R - 迄今为止的午夜 POSIXct

[英]R - midnight POSIXct to date

If I have midnight (or even around midnight posixct) date like this如果我有这样的午夜(甚至午夜 posixct)日期

> as.POSIXct("2020-01-01 00:00:00")
[1] "2020-01-01 CET"

But if I transfer it to the date I get one day before this date但是如果我把它转移到我在这个日期前一天得到的日期

> as.Date(as.POSIXct("2020-01-01 00:00:00"))
[1] "2019-12-31"

In case that hour is lets say 8am etc, as.Date works properly.如果那个小时是早上 8 点等,as.Date 可以正常工作。 How can I get 2020-01-01 date for my example where hour is midnight?对于我的示例,小时是午夜,如何获得 2020-01-01 日期?

You need to specify the timezone again.您需要再次指定时区。

As you can see in ?as.Date :正如你在?as.Date看到的:

## S3 method for class 'POSIXct' ## 'POSIXct' 类的 S3 方法

as.Date(x, tz = "UTC", ...) as.Date(x, tz = "UTC", ...)

The function defaults to UTC.该函数默认为 UTC。

as.Date(as.POSIXct("2020-01-01 00:00:00",tz="CET"),tz="CET")
#[1] "2020-01-01"

as.Date(as.POSIXct("2020-01-01 00:00:00",tz="CET"))
#[1] "2019-12-31"

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

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