Date Selector

#| standalone: true
#| components: [viewer]
#| viewerHeight: 380

library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateInput("date", label = h3("Date input"), value = Sys.Date()),
  hr(),
  verbatimTextOutput("value"),
  verbatimTextOutput("value_class"),
  verbatimTextOutput("value_year")
)

server <- function(input, output, session) {
  # You can access the value of the widget with input$date
  output$value <- renderPrint({ input$date })
  output$value_class <- renderPrint({ class(input$date) })
  output$value_year <- renderPrint({ year(input$date) })
}

shinyApp(ui, server)
library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateInput(
    inputId = "date",
    label = h3("Date input"),
    value = Sys.Date()),
  hr(),
  verbatimTextOutput("value"),
  verbatimTextOutput("value_class"),
  verbatimTextOutput("value_year")
)

server <- function(input, output, session) {
  # You can access the value of the widget with input$date
  output$value <- renderPrint({ input$date })
  output$value_class <- renderPrint({ class(input$date) })
  output$value_year <- renderPrint({ year(input$date) })
}

shinyApp(ui, server)
No matching items

Relevant Functions

  • dateInput()
    dateInput(inputId, label, value = NULL, min = NULL, max = NULL, format = "yyyy-mm-dd", startview = "month", weekstart = 0, language = "en", width = NULL, autoclose = TRUE, datesdisabled = NULL, daysofweekdisabled = NULL)

  • dateRangeInput()
    dateRangeInput(inputId, label, start = NULL, end = NULL, min = NULL, max = NULL, format = "yyyy-mm-dd", startview = "month", weekstart = 0, language = "en", separator = " to ", width = NULL, autoclose = TRUE)

  • updateDateRangeInput()
    updateDateRangeInput(session = getDefaultReactiveDomain(), inputId, label = NULL, start = NULL, end = NULL, min = NULL, max = NULL)

No matching items

Details

A date selector allows you to select a date from a calendar.

To add a date selector to your app:

  1. Add dateInput() to the UI of your app to create a date selector. Where you call this function will determine where the date selector will appear within the app’s layout.

  2. Specify the inputId and label parameters of dateInput() to define the identifier and label of the date selector. dateInput() also includes various optional parameters, including min and max, which set the minimum and maximum allowed dates.

The value of an input component is accessible as a reactive value within the server() function. To access the value of a date selector:

  1. Use input$<date_id>() to access the value of a daterange selector (e.g., input$date()). The server value of a date selector is a date object.

The date format string specifies how the date will be displayed in the browser. It allows the following values:

  • yy Year without century (12)
  • yyyy Year with century (2012)
  • mm Month number, with leading zero (01-12)
  • m Month number, without leading zero (1-12)
  • M Abbreviated month name
  • MM Full month name
  • dd Day of month with leading zero
  • d Day of month without leading zero
  • D Abbreviated weekday name
  • DD Full weekday name

Variations

Minimum and maximum date range

Limit the range of possible dates that can be selected with the min and max parameters.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateInput(
    "date",
    label = h3("Date input"),
    value = Sys.Date(),
    min = Sys.Date() - 7,
    max = Sys.Date() + 7
  )
)

server <- function(input, output, session) {

}

shinyApp(ui, server)
library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateInput(
    "date",
    label = h3("Date input"),
    value = Sys.Date(),
    min = Sys.Date() - 7,
    max = Sys.Date() + 7
  )
)

server <- function(input, output, session) {

}

shinyApp(ui, server)

Date displayed format

Change the date displayed by the component. The data type returned by the component for programatic purposes remain the same.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateInput(
    "date",
    label = h3("Date input"),
    value = Sys.Date(),
    format = "MM dd, yyyy"
  )
)

server <- function(input, output, session) {

}

shinyApp(ui, server)
library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateInput(
    "date",
    label = h3("Date input"),
    value = Sys.Date(),
    format = "MM dd, yyyy"
  )
)

server <- function(input, output, session) {

}

shinyApp(ui, server)

Start view

When clicked, the level where the dates are displayed. Defaults to “month”. Other possible values are “year” and “decade”.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateInput(
    "date",
    label = h3("Date input"),
    value = Sys.Date(),
    startview = "year"
  )
)

server <- function(input, output, session) {

}

shinyApp(ui, server)
library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateInput(
    "date",
    label = h3("Date input"),
    value = Sys.Date(),
    startview = "year"
  )
)

server <- function(input, output, session) {

}

shinyApp(ui, server)

Week start

Change the first day of the week. 0 for Sunday, 6 for Saturday.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateInput(
    "date",
    label = h3("Date input"),
    value = Sys.Date(),
    weekstart = 1
  )
)

server <- function(input, output, session) {

}

shinyApp(ui, server)
library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateInput(
    "date",
    label = h3("Date input"),
    value = Sys.Date(),
    weekstart = 1
  )
)

server <- function(input, output, session) {

}

shinyApp(ui, server)

Language

Change the language used in the calendar.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateInput(
    "date",
    label = h3("Date input"),
    value = Sys.Date(),
    format = "MM dd, yyyy",
    language = "fr-CH"
  )
)

server <- function(input, output, session) {

}

shinyApp(ui, server)
library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateInput(
    "date",
    label = h3("Date input"),
    value = Sys.Date(),
    format = "MM dd, yyyy",
    language = "fr-CH"
  )
)

server <- function(input, output, session) {

}

shinyApp(ui, server)

Disable dates

Disable either specific dates or days of the week.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateInput(
    "date",
    label = h3("Date input"),
    value = Sys.Date(),
    min = Sys.Date() -14,
    max = Sys.Date() + 14,
    datesdisabled = c(
      Sys.Date() - 2,
      Sys.Date() - 1,
      Sys.Date(),
      Sys.Date() + 7),
    daysofweekdisabled = c(0, 6)
  )
)

server <- function(input, output, session) {

}

shinyApp(ui, server)
library(bslib)
library(shiny)
library(lubridate)

ui <- page_fixed(
  dateInput(
    "date",
    label = h3("Date input"),
    value = Sys.Date(),
    min = Sys.Date() -14,
    max = Sys.Date() + 14,
    datesdisabled = c(
      Sys.Date() - 2,
      Sys.Date() - 1,
      Sys.Date(),
      Sys.Date() + 7),
    daysofweekdisabled = c(0, 6)
  )
)

server <- function(input, output, session) {

}

shinyApp(ui, server)
No matching items