Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract correct date from POSIXct element? [duplicate]

How can I get the correct date from the first column in my code?

test <- data.frame(posixdate = c("2013-05-01 00:59:00", "2013-05-01 01:59:00", "2013-05-01 02:59:00", "2013-05-01 03:59:00"))
test$posixdate <- as.POSIXct(test$posixdate, format="%Y-%m-%d %H:%M:%S" )
test$date <- as.Date(test$posixdate)

The above code results in:

  posixdate           date
1 2013-05-01 00:59:00 2013-04-30
2 2013-05-01 01:59:00 2013-04-30
3 2013-05-01 02:59:00 2013-05-01
4 2013-05-01 03:59:00 2013-05-01

The first two dates are not correct. What did I do wrong?
If as.Date() is not the right function, how could I get the date (without hours, minutes, seconds) alternatively?

like image 537
Aki Avatar asked Mar 23 '15 09:03

Aki


1 Answers

the problem is the timezone

try your timezone (probably not GMT)

test$date2 <- as.Date(test$posixdate, "GMT")

and read this post

like image 119
rmuc8 Avatar answered Sep 21 '22 17:09

rmuc8