how to parse R shiny dates in a specific format

Viewed 123

Im trying to get some date ranges from a calendar input. These dates need to be formatted as text like this:

If user picks May 6 / 1986 from the calendar, i will require 19860506 so i can use this text string on other calculations.

This is a basic example of what i have so far but Im not getting the desired result

UI

ui <- fluidPage(
           airDatepickerInput(
             inputId = "range",
             label = "Select range of dates:",
             range = TRUE, 
             value = c(Sys.Date()-7, 
                       Sys.Date()),
             todayButton = F,
             clearButton = T,
             addon = c("none")
            ) )

Server

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

    date_start <- as.character(as.Date(input$range[1], format="%Y%m%d"))
    date_end   <- as.character(as.Date(input$range[2], format="%Y%m%d"))

    ##Visualization check
    output$date1  <- renderPrint(date_start)
    output$date2  <- renderPrint(date_end)
    }
1 Answers

The format function worked for me

date_start <- input$range[1]
date_end   <- input$range[2]

date_start = format(date_start, "%Y%m%d")   
date_end = format(date_end, "%Y%m%d") 
    
output$date1  <- renderText(date_start)
output$date2  <- renderText(date_end)
Related