Submission by Mohd Anas Ahmad (s2001089)

Importing library

The UI

ui <-fluidPage(pageWithSidebar(
  headerPanel("This Shiny apps calculate the time difference between two dates"),
  sidebarPanel(
    h5("Enters the two date and time of which you wish to calculate the difference."),
    dateRangeInput("date", "Please enter the date :",
                   format = "dd-mm-yyyy"),
  ),
  mainPanel(
    h5('The time difference is:'),
    verbatimTextOutput("weeks"),
    verbatimTextOutput("day"),
    verbatimTextOutput("weekdays")
  )
))

The Server

server <- function(input, output) {
  output$weeks <- renderPrint({renderTotalWeeks(input$date[1], input$date[2])})
  output$day <- renderPrint({renderTotalDays(input$date[1], input$date[2])})
  output$weekdays <- renderPrint({renderTotalWeekDays(input$date[1], input$date[2])})
  print(input)
  
  renderTotalWeeks <- function(d1,d2){
    weeks <- as.numeric(difftime(d2,d1,units="weeks"))
    return(cat("Total week(s):",abs(weeks)))
  }

  
  renderTotalDays <- function(d1,d2){
    days <- as.integer(difftime(d2,d1,units="days"))
    return(cat("Total days(s):",abs(days)))
  }

  
  renderTotalWeekDays <- function(d1,d2){
    
    weeks <- abs(as.integer(difftime(d2,d1,units="weeks")))
    days <- abs(as.integer(difftime(d2,d1,units="days")))%%14

    return(cat(weeks,"week(s)","and",days,"day(s)"))
    
  }
  
}

Running the App

shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents