# Generate Random Sample of POSIXct Dates & Times in R (Example)

In this tutorial, Iâ€™ll show how to **sample random dates and times** in R programming.

The tutorial will contain the following content:

Letâ€™s get started.

## Example: Sample Random POSIXct Dates & Times Using seq() & sample() Functions

This example shows how to generate a random sample of POSIXct dates and times.

For this task, we first have to specify a sequence of dates and times between a certain range and with a particular time interval:

datetime_sequence <- seq(as.POSIXct('2020/01/01'), # Create sequence of dates as.POSIXct('2030/01/01'), by = "10 mins") head(datetime_sequence) # Print head of sequence of dates # [1] "2020-01-01 00:00:00 UTC" "2020-01-01 00:10:00 UTC" # [3] "2020-01-01 00:20:00 UTC" "2020-01-01 00:30:00 UTC" # [5] "2020-01-01 00:40:00 UTC" "2020-01-01 00:50:00 UTC"

Furthermore, we should set a random seed for reproducibility:

set.seed(3487635) # Set random seed

In the next step, we can randomly draw dates and times from our sequence using the sample function:

datetime_random <- sample(datetime_sequence, # Sample random dates from sequence size = 500, replace = TRUE) head(datetime_random) # Print head of random dates # [1] "2029-04-29 02:50:00 UTC" "2026-02-28 17:30:00 UTC" # [3] "2021-04-07 04:00:00 UTC" "2026-07-01 11:50:00 UTC" # [5] "2025-09-03 21:50:00 UTC" "2020-10-31 10:40:00 UTC"

The previous output shows the first six dates and times of our random sample.

## Video, Further Resources & Summary

Would you like to know more about the sampling of random dates and times? Then you might want to have a look at the following video on my YouTube channel. Iâ€™m demonstrating the contents of this page in the video.

Furthermore, you may want to read the other tutorials on my homepage:

- Generate Multivariate Random Data in R
- Generate Set of Random Integers from Interval
- Sample Random Rows of Data Frame
- Generate Matrix with i.i.d. Normal Random Variables
- R Programming Examples

In this tutorial you have learned how to **draw a random sample of dates and times** in R programming. If you have further questions, tell me about it in the comments.