On Fri, 22 Apr 2005, Ghosh, Sandeep wrote:

For the r script below

datestr <- "01/01/2004"
as.POSIXct(as.Date(datestr, "%d/%m/%Y"))
I get the following output
"2003-12-31 18:00:00 Central Standard Time"

Why is the date a day before. I guess its something to do with the time, but is there a way to get it to return 2004-01-01 instead?

Yes.

If you want POSIXt, which includes time as well as date and so depends on time zone, use it.
datestr <- "01/01/2004"
strptime(datestr, "%d/%m/%Y")
[1] "2004-01-01"
class(strptime(datestr, "%d/%m/%Y"))
[1] "POSIXt" "POSIXlt"
as.POSIXct(strptime(datestr, "%d/%m/%Y"))
[1] "2004-01-01 Pacific Standard Time"

If you just want a date, just use Date
as.Date(datestr, "%d/%m/%Y")
[1] "2004-01-01"

When you convert from Date to POSIXt it has to pick an arbitrary time within the day, and it picks midnight GMT.

        -thomas

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to