0

I'm trying to calculate the difference between two dates using a dateRangeInput. I can use both of the dates as a value but when I try to subtract them using difftime nothing happens.

Here's my code:

    date1 <- input$daterange[2]
    date2 <- input$daterange[1]
    date3 <- difftime(date2, date1, units = "days")

Here you can see how the value "date3" is blank:

Value date3 stays blank

What am I doing wrong here?

EDIT:
Here's the hard coded version. Copy and paste it to try it out. Doesn't work either.

date1 <- as.Date("2016-01-01")
date2 <- as.Date("2017-01-01")
date3 <- difftime(date2, date1, units = "days")
  • 2
    may be because `dateRangeInput` return `character`? try `asDate` – Batanichek Jan 29 '16 at 14:51
  • 2
    Welcome to SO. This would be a better post if you hard coded `date1` and `date2`. That way, everyone would know if the problem was with how you're getting the dates, or if the problem was with how you're using `difftime`. – Teepeemm Jan 29 '16 at 14:52
  • 1
    Please provide a minimal, complete, and reproducible example that anyone else can simply copy and paste into their R session to run. All library statements and inputs need to be included. Cut down your data to the minimum needed to illustrate the problem and if your input is `x` then show it by displaying the output of `dput(x)` in your question. See http://stackoverflow.com/help/mcve for general advice and see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for more R oriented advice on how to ask a question on SO. – G. Grothendieck Jan 29 '16 at 15:01
  • 2
    I assume this has to do with how RStudio decides what to show in the Environment pane. Just print `date3` in the console and you should get the output `Time difference of 366 days`. – Stibu Jan 29 '16 at 15:13
  • It works in the console, I know. However, I want date3 to be an integer so I'm able to do further calculations with it. – Tim Krombholz Jan 29 '16 at 15:22
  • 1
    `as.integer(date3)`? Btw: you can do further calculations on `date3`. Try for example: `date3 * 2` – Jaap Jan 29 '16 at 15:26

0 Answers0