I hit a similar issue recently using the Javascript Date
constructor to parse dates in yyyy-mm-dd
format. Apparently date string parsing in Javascript is just really wacky, with gotchas and browser inconsistencies all over. From what I understand, some browsers parse yyyy-mm-dd
strings as ISO 8601 dates in UTC, not the local timezone. When that gets converted back to the local timezone, it may be a different day.
From the MDN docs
Note: parsing of date strings with the Date
constructor (and Date.parse
, they are equivalent) is strongly discouraged due to browser differences and inconsistencies. Support for RFC 2822 format strings is by convention only. Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local.
The dygraphs blog also has a great write up with advice on date formats to use.
http://dygraphs.com/date-formats.html
I think somewhere in the dateRangeInput (and also dateInput), date strings get parsed the same way. These formats seem to work around the issue in both the RStudio Viewer and Chrome. Should work on other browsers too, but I haven't tested to be 100% sure.
library(shiny)
ui <- fluidPage(
# doesn't work
dateRangeInput("dates",
label = "Dates",
start = "1789-04-14",
end = "1791-03-07",
min = "1789-04-14",
max = "1791-03-07"
),
# slashes instead of dashes, works
dateRangeInput("dates",
label = "Dates",
start = "1789-04-14",
end = "1791-03-07",
min = "1789-04-14",
max = "1791/03/07"
),
# add a time component, works (ISO 8601 format using local time, not UTC)
dateRangeInput("dates",
label = "Dates",
start = "1789-04-14",
end = "1791-03-07",
min = "1789-04-14",
max = "1791-03-07T00:00:00"
),
# same issue and workaround with the dateInput
dateInput("d", "date", value = "1789-03-09", max = "1789-03-09"),
dateInput("d", "date", value = "1789-03-09", max = "1789/03/09")
)
server <- function(input, output) {}
shinyApp(ui, server)