[R] as.Date() add a day to a date

David Winsemius dwinsemius at comcast.net
Mon Sep 13 20:15:29 CEST 2010


Try this:

format(dt, '%Y-%m-%d'), if you want Date class:

as.Date(format(dt, '%Y-%m-%d'))



> On Mon, Sep 13, 2010 at 2:24 PM, Andrew Yee <yee at post.harvard.edu>  
> wrote:
> Thanks David, now I wonder how you can have as.Date() render the  
> date using
> local time rather than UTC.
>
> Andrew
>
> On Mon, Sep 13, 2010 at 12:08 PM, David Winsemius <dwinsemius at comcast.net 
> >wrote:
>
> >
> > On Sep 13, 2010, at 11:56 AM, Andrew Yee wrote:
> >
> >  I'm trying to understand why as.Date() is converting a the  
> modified date
> >> of
> >> a file from August 22 to August 23.
> >>
> >>  foo <- file.info(file.to.process)
> >>> str(foo)
> >>>
> >> 'data.frame':   1 obs. of  10 variables:
> >> $ size  : num 5.37e+09
> >> $ isdir : logi FALSE
> >> $ mode  :Class 'octmode'  int 436
> >> $ mtime : POSIXct, format: "2010-08-22 23:14:52"
> >> $ ctime : POSIXct, format: "2010-09-13 11:34:31"
> >> $ atime : POSIXct, format: "2010-09-09 15:42:00"
> >> $ uid   : int 503
> >> $ gid   : int 503
> >> $ uname : chr "ayee"
> >> $ grname: chr "ayee"
> >>
> >>  as.Date(foo$mtime)
> >>>
> >> [1] "2010-08-23"
> >>
> >> In this case as.Date(foo$mtime) gives August 23.  I would have  
> expected
> >> August 22.
> >>
> >
> > You are pretty close to midnight. Probably a TZ issue. Check your  
> locale
> > settings versus UTC/GMT.
> >
> > >  dt <- as.POSIXct("2010-08-22 23:14:52")
> > > as.Date(dt)
> > [1] "2010-08-23"
> >
> > Happens to me too, but when its 23:14 here, (US-EDT) it's the next  
> day in
> > Greenwich. The help page says:
> > "The last [POSIXct] is converted to days by ignoring the time after
> > midnight in the representation of the time in UTC." That is not  
> entirely
> > clear to my reading, but I am forced to conclude that it means  
> as.Date
> > returns the Date in UTC terms rather than in local terms.
> >
> > --
> > David.
> >
> >
> >> Thanks,
> >> Andrew
> >>
> >>  sessionInfo()
> >>>
> >> R version 2.11.1 Patched (2010-09-11 r52901)
> >> Platform: x86_64-unknown-linux-gnu (64-bit)
> >>
> >> locale:
> >> [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
> >> [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
> >> [5] LC_MONETARY=C              LC_MESSAGES=en_US.UTF-8
> >> [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
> >> [9] LC_ADDRESS=C               LC_TELEPHONE=C
> >> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
> >>
> >> attached base packages:
> >> [1] stats     graphics  grDevices utils     datasets  methods    
> base
> >>
> >>        [[alternative HTML version deleted]]
> >>

-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list