29

From two integers (1, 5) one can create a range in the following way

1:5

[1] 1 2 3 4 5

How can you make a range of dates if you are give two dates ("2014-09-04 JST", "2014-09-11 JST")

The output must be

[1] ("2014-09-04 JST", "2014-09-05 JST", "2014-09-06 JST", "2014-09-07 JST", "2014-09-08 JST")

5
  • 1
    Well, if you have a timezone, then they are not Dates but rather datetimes. There are seq-methods for either class. Commented Sep 5, 2014 at 0:43
  • Can you answer this question for date + timezone? Commented Sep 5, 2014 at 0:49
  • @sjdh See comment to jalapic answer. Commented Sep 5, 2014 at 0:54
  • @sjdh - would be great if you could give an answer a tick to close the question. thanks. Commented Oct 1, 2014 at 13:20
  • Good solutions here, too: stackoverflow.com/questions/14450384/… Commented Feb 12, 2019 at 22:16

5 Answers 5

46

Does this help?

seq(as.Date("2014/09/04"), by = "day", length.out = 5)
# [1] "2014-09-04" "2014-09-05" "2014-09-06" "2014-09-07" "2014-09-08"

edit: adding in something about timezones

this works for my current timezone

seq(c(ISOdate(2014,4,9)), by = "DSTday", length.out = 5) 
#[1] "2014-04-09 08:00:00 EDT" "2014-04-10 08:00:00 EDT" "2014-04-11 08:00:00 EDT" "2014-04-12 08:00:00 EDT"
#[5] "2014-04-13 08:00:00 EDT"

edit2:

OlsonNames()  # I used this to find out what to write for the JST tz - it's "Japan"

x <- as.POSIXct("2014-09-04 23:59:59", tz="Japan")
format(seq(x, by="day", length.out=5), "%Y-%m-%d %Z")

# [1] "2014-09-04 JST" "2014-09-05 JST" "2014-09-06 JST" "2014-09-07 JST" "2014-09-08 JST"
Sign up to request clarification or add additional context in comments.

3 Comments

How about format( seq(c(ISOdate(2014,4,9)), by = "DSTday", length.out = 5), "%Y-%m-%d %Z")
If I give you these two dates ("2014-09-04 JST", "2014-09-11 JST") can you construct a sequence too? With result in JST? (In case you are curious, JST stands for Japanese Standard Time)
Not sure why this was accepted. It doesn't answer the question. "How can you make a range of dates if you are give (sic) two dates?"
24

To get a sequence of dates ( days, weeks,.. ) using only start and end dates you can use:

seq(as.Date("2014/1/1"), as.Date("2014/1/10"), "days”)

[1] "2014-01-01" "2014-01-02" "2014-01-03" "2014-01-04" "2014-01-05" "2014-01-06" "2014-01-07"
[8] "2014-01-08" "2014-01-09" "2014-01-10”

2 Comments

seq(as.Date("2014/1/1"), as.Date("2014/1/10"),by="day")
Also, the original had smart quotes doing their evil business.
2

While using seq(date1, date2, "days") is by far the better option in nearly all cases, I'd just like to add, that the following works too, if you need a range of dates that are n_number of days from a date:

1:10 + as.Date("2020-01-01")

# [1] "2020-01-02" "2020-01-03" "2020-01-04" "2020-01-05"
# [5] "2020-01-06" "2020-01-07" "2020-01-08" "2020-01-09"
# [9] "2020-01-10" "2020-01-11"

1 Comment

Wow! This is a beautiful answer and deserves many more votes . To anyone wondering why this works, it is vectorized addition at work, very similar to operations like 1:10 +2 or 1:10 * 2 .
0

Here's an answer, admittedly worse than @jalapic's, that doesn't use seq and instead uses a for loop:

date1 <- "2014-09-04"
date2 <- "2014-09-11"
dif <- as.numeric(abs(as.Date(date1) - as.Date(date2)))
dates <- vector()
for (i in 1:dif) {
  date <- (as.Date(date1) + i)
  dates <- append(dates, date)
}
# [1] "2014-09-05" "2014-09-06" "2014-09-07" "2014-09-08" "2014-09-09" "2014-09-10" "2014-09-11

1 Comment

There's no reason to use a loop for this.
0

here's a shot though the timezone JST isn't recognized by my system

d1<-ISOdate(year=2014,month=9,day=4,tz="GMT")
seq(from=d1,by="day",length.out=5)
[1] "2014-09-04 12:00:00 GMT" "2014-09-05 12:00:00 GMT" "2014-09-06 12:00:00 GMT" "2014-09-07 12:00:00 GMT" "2014-09-08 12:00:00 GMT"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.