Create a Vector of All Days Between Two Dates
You're looking for seq
> seq(as.Date("2011-12-30"), as.Date("2012-01-04"), by="days")
[1] "2011-12-30" "2011-12-31" "2012-01-01" "2012-01-02" "2012-01-03"
[6] "2012-01-04"
Or, you can use :
> as.Date(as.Date("2011-12-30"):as.Date("2012-01-04"), origin="1970-01-01")
[1] "2011-12-30" "2011-12-31" "2012-01-01" "2012-01-02" "2012-01-03"
[6] "2012-01-04"
Note that with :
"Non-numeric arguments are coerced internally". Thus, we convert back to class Date
, using as.Date
method for class 'numeric' and provide origin
.
Here's a function to meet your specific request
itemizeDates <- function(startDate="12-30-11", endDate="1-4-12",
format="%m-%d-%y") {
out <- seq(as.Date(startDate, format=format),
as.Date(endDate, format=format), by="days")
format(out, format)
}
> itemizeDates(startDate="12-30-11", endDate="1-4-12")
[1] "12-30-11" "12-31-11" "01-01-12" "01-02-12" "01-03-12" "01-04-12"
Javascript - get array of dates between 2 dates
function (startDate, endDate, addFn, interval) {
addFn = addFn || Date.prototype.addDays;
interval = interval || 1;
var retVal = [];
var current = new Date(startDate);
while (current <= endDate) {
retVal.push(new Date(current));
current = addFn.call(current, interval);
}
return retVal;
}
How to create a vector between 2 dates in R
The key is to use proper types. Never ever use characters for dates.
R> p1 <- as.Date("2011-01-01")
R> p2 <- as.Date("2012-12-31")
R> mydates <- seq(p1, p2, by="day")
R> head(mydates)
[1] "2011-01-01" "2011-01-02" "2011-01-03" "2011-01-04" "2011-01-05" "2011-01-06"
R> str(mydates)
Date[1:731], format: "2011-01-01" "2011-01-02" "2011-01-03" "2011-01-04" "2011-01-05" "2011-01-06" "2011-01-07" ...
R>
For the proper types Date
and POSIXct
(for Datetime), R has a lot of useful computational infrastructure. As you see here, sequence creation "just works".
You can then do additional tricks like excluding weekends etc.
Python generating a list of dates between two dates
You can use pandas.date_range()
for this:
import pandas
pandas.date_range(sdate,edate-timedelta(days=1),freq='d')
DatetimeIndex(['2019-03-22', '2019-03-23', '2019-03-24', '2019-03-25',
'2019-03-26', '2019-03-27', '2019-03-28', '2019-03-29',
'2019-03-30', '2019-03-31', '2019-04-01', '2019-04-02',
'2019-04-03', '2019-04-04', '2019-04-05', '2019-04-06',
'2019-04-07', '2019-04-08'],
dtype='datetime64[ns]', freq='D')
How do I create a vector of random dates between two time points in R?
start_date <- as.Date('2015-01-01')
end_date <- as.Date('2017-01-01')
set.seed(1984)
as.Date(sample( as.numeric(start_date): as.numeric(end_date), 10,
replace = T),
origin = '1970-01-01')
[1] "2016-04-27" "2015-11-16" "2015-10-01" "2015-08-31" "2016-06-23"
[6] "2016-09-23" "2015-01-24" "2015-11-24" "2016-08-30" "2015-06-07"
Counting the number of days excluding Sundays between two dates and creating a new column in R DataFrame
I realized with an updated data set that there's a problem with the solution above, when Start-Date and End-Date aren't in the same year. I still want to count the days except Sundays starting on the 20.12.2020 until 10.01.2021 for example. The error message showing up in that case is that the sign with the argument "by" is wrong. I just can't manage to get it running . If I turn the dates around, the output makes no sense and the number of days is too high. What do I have to do to get this running over the year-end?
R - how do I declare a vector of Date?
You can use a sequence, or just just add:
R> seq( as.Date("2011-07-01"), by=1, len=3)
[1] "2011-07-01" "2011-07-02" "2011-07-03"
R> as.Date("2011-07-01") + 0:2
[1] "2011-07-01" "2011-07-02" "2011-07-03"
R>
and that both work the same way is a nice illustration of why object-orientation is nice for programming with data.
Date, as you saw, has an underlying numeric representation (of integers representing the number of days since the beginning of Unix time, aka Jan 1, 1970) but it also has a class attribute which makes the formatting, arithmetic, ... behave the way it does utilising the dispatch mechanism in R.
Edit: By the same token, you can also start with a standard vector and turn it into a Date
object:
R> x <- 1:3
R> class(x) <- "Date"
R> x
[1] "1970-01-02" "1970-01-03" "1970-01-04"
R>
Creating a range of dates in Python
Marginally better...
base = datetime.datetime.today()
date_list = [base - datetime.timedelta(days=x) for x in range(numdays)]
Related Topics
Get Rid of \Addlinespace in Kable
Using Two Scale Colour Gradients on One Ggplot
Removing One Tablegrob When Applied to a Box Plot with a Facet_Wrap
Merge Panel Data to Get Balanced Panel Data
How 'Poly()' Generates Orthogonal Polynomials? How to Understand the "Coefs" Returned
Any Suggestions for How to Plot Mixem Type Data Using Ggplot2
Rscript: There Is No Package Called ...
How to Make Time Difference in Same Units When Subtracting Posixct
Different Breaks Per Facet in Ggplot2 Histogram
Shift Values in Single Column of Dataframe Up
Rstudio Shiny Error: There Is No Package Called "Shinydashboard"
How to Change Type of Target Column When Doing := by Group in a Data.Table in R
Difference Between If() and Ifelse() Functions
Read/Write Data in Libsvm Format
Cbind 2 Dataframes with Different Number of Rows
How to Make Variable Bar Widths in Ggplot2 Not Overlap or Gap