1

I have created two variables that represent the min and max of yesterday's time:

yesterday_min  <- force_tz(as.POSIXct(sprintf('%s 00:00:00', Sys.Date()-1)), 'America/New_York')
yesterday_max  <- force_tz(as.POSIXct(sprintf('%s 00:00:00', Sys.Date())), 'America/New_York') - 1L

> yesterday_min
[1] "2018-01-15 EST"

> yesterday_max
[1] "2018-01-15 23:59:59 EST"

> attr(yesterday_min,"tzone")
[1] "America/New_York"

> attr(yesterday_max,"tzone")
[1] "America/New_York"

They are both on the same date, same timezone. However, the function as.Date() recognizes them as two separate dates. Expected behavior is for both dates to be "2018-01-15". Why does this happen and how would one solve this problem?

> as.Date(yesterday_min)
[1] "2018-01-15"

> as.Date(yesterday_max)
[1] "2018-01-16"
iskandarblue
  • 7,208
  • 15
  • 60
  • 130
  • 2
    Related? [Date conversion from POSIXct to Date in R](https://stackoverflow.com/questions/16557028/date-conversion-from-posixct-to-date-in-r); [as.Date(as.POSIXct()) gives the wrong date?](https://stackoverflow.com/questions/17098862/as-dateas-posixct-gives-the-wrong-date) (rather google-able, I just tried "r as.POSIXct as.Date wrong date"). – Henrik Jan 16 '18 at 20:56
  • These are related, but the answers do not apply. The timezone is defined above – iskandarblue Jan 16 '18 at 21:00
  • You’ve specified the timezone for the POSIXct object but as.Date.POSIXct reports the date in UTC by default. You can solve with as.Date(yesterday_max, tz = 'America/New_York') – Paul Jan 16 '18 at 23:27

0 Answers0