How to convert a date or date vector to POSIXct in R?

R ProgrammingServer Side ProgrammingProgramming

To convert a date or date vector to POSIXct, we can use as.POSIXct function but we also need to pass the appropriate date format inside the function. For example, if we have a date "2020-11-14" then it can be converted to POSIXct by using as.POSIXct("2020-11-14",format="%Y-%m-%d").

Example1

Live Demo

> date1<-"2020-04-01"
> as.POSIXct(date1,format="%Y-%m-%d")

Output

[1] "2020-04-01 IST"

Example2

Live Demo

> date2<-sample(c("2020-04-01","2020-10-01","2020-02-12","2020-04-11","2020-01-21","2020-04-27","2020-03-31","2020-05-01","2020-05-11","2020-06-01"),60,replace=TRUE)
> date2

Output

[1] "2020-02-12" "2020-06-01" "2020-04-01" "2020-05-01" "2020-01-21"
[6] "2020-01-21" "2020-06-01" "2020-04-27" "2020-05-11" "2020-06-01"
[11] "2020-01-21" "2020-03-31" "2020-05-01" "2020-02-12" "2020-01-21"
[16] "2020-05-01" "2020-03-31" "2020-04-01" "2020-05-01" "2020-01-21"
[21] "2020-05-01" "2020-04-11" "2020-05-11" "2020-04-01" "2020-03-31"
[26] "2020-04-11" "2020-04-01" "2020-03-31" "2020-04-01" "2020-04-11"
[31] "2020-05-11" "2020-06-01" "2020-03-31" "2020-04-27" "2020-01-21"
[36] "2020-01-21" "2020-04-01" "2020-06-01" "2020-05-01" "2020-10-01"
[41] "2020-04-27" "2020-01-21" "2020-10-01" "2020-01-21" "2020-04-01"
[46] "2020-04-11" "2020-10-01" "2020-10-01" "2020-05-11" "2020-02-12"
[51] "2020-05-01" "2020-05-01" "2020-01-21" "2020-01-21" "2020-02-12"
[56] "2020-05-01" "2020-02-12" "2020-02-12" "2020-04-11" "2020-10-01"

Example

> as.POSIXct(date2,format="%Y-%m-%d")

Output

[1] "2020-02-12 IST" "2020-06-01 IST" "2020-04-01 IST" "2020-05-01 IST"
[5] "2020-01-21 IST" "2020-01-21 IST" "2020-06-01 IST" "2020-04-27 IST"
[9] "2020-05-11 IST" "2020-06-01 IST" "2020-01-21 IST" "2020-03-31 IST"
[13] "2020-05-01 IST" "2020-02-12 IST" "2020-01-21 IST" "2020-05-01 IST"
[17] "2020-03-31 IST" "2020-04-01 IST" "2020-05-01 IST" "2020-01-21 IST"
[21] "2020-05-01 IST" "2020-04-11 IST" "2020-05-11 IST" "2020-04-01 IST"
[25] "2020-03-31 IST" "2020-04-11 IST" "2020-04-01 IST" "2020-03-31 IST"
[29] "2020-04-01 IST" "2020-04-11 IST" "2020-05-11 IST" "2020-06-01 IST"
[33] "2020-03-31 IST" "2020-04-27 IST" "2020-01-21 IST" "2020-01-21 IST"
[37] "2020-04-01 IST" "2020-06-01 IST" "2020-05-01 IST" "2020-10-01 IST"
[41] "2020-04-27 IST" "2020-01-21 IST" "2020-10-01 IST" "2020-01-21 IST"
[45] "2020-04-01 IST" "2020-04-11 IST" "2020-10-01 IST" "2020-10-01 IST"
[49] "2020-05-11 IST" "2020-02-12 IST" "2020-05-01 IST" "2020-05-01 IST"
[53] "2020-01-21 IST" "2020-01-21 IST" "2020-02-12 IST" "2020-05-01 IST"
[57] "2020-02-12 IST" "2020-02-12 IST" "2020-04-11 IST" "2020-10-01 IST"

Example3

Live Demo

> date3<-rep(c("2020-04-01","2020-10-01","2020-02-12","2020-04-11","2020-01-21","2020-04-27","2020-03-31","2020-05-01","2020-05-11","2020-06-01"),10)
> date3

Output

[1] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[6] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[11] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[16] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[21] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[26] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[31] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[36] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[41] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[46] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[51] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[56] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[61] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[66] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[71] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[76] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[81] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[86] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[91] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[96] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"

Example

> as.POSIXct(date3,format="%Y-%m-%d")

Output

[1] "2020-04-01 IST" "2020-10-01 IST" "2020-02-12 IST" "2020-04-11 IST"
[5] "2020-01-21 IST" "2020-04-27 IST" "2020-03-31 IST" "2020-05-01 IST"
[9] "2020-05-11 IST" "2020-06-01 IST" "2020-04-01 IST" "2020-10-01 IST"
[13] "2020-02-12 IST" "2020-04-11 IST" "2020-01-21 IST" "2020-04-27 IST"
[17] "2020-03-31 IST" "2020-05-01 IST" "2020-05-11 IST" "2020-06-01 IST"
[21] "2020-04-01 IST" "2020-10-01 IST" "2020-02-12 IST" "2020-04-11 IST"
[25] "2020-01-21 IST" "2020-04-27 IST" "2020-03-31 IST" "2020-05-01 IST"
[29] "2020-05-11 IST" "2020-06-01 IST" "2020-04-01 IST" "2020-10-01 IST"
[33] "2020-02-12 IST" "2020-04-11 IST" "2020-01-21 IST" "2020-04-27 IST"
[37] "2020-03-31 IST" "2020-05-01 IST" "2020-05-11 IST" "2020-06-01 IST"
[41] "2020-04-01 IST" "2020-10-01 IST" "2020-02-12 IST" "2020-04-11 IST"
[45] "2020-01-21 IST" "2020-04-27 IST" "2020-03-31 IST" "2020-05-01 IST"
[49] "2020-05-11 IST" "2020-06-01 IST" "2020-04-01 IST" "2020-10-01 IST"
[53] "2020-02-12 IST" "2020-04-11 IST" "2020-01-21 IST" "2020-04-27 IST"
[57] "2020-03-31 IST" "2020-05-01 IST" "2020-05-11 IST" "2020-06-01 IST"
[61] "2020-04-01 IST" "2020-10-01 IST" "2020-02-12 IST" "2020-04-11 IST"
[65] "2020-01-21 IST" "2020-04-27 IST" "2020-03-31 IST" "2020-05-01 IST"
[69] "2020-05-11 IST" "2020-06-01 IST" "2020-04-01 IST" "2020-10-01 IST"
[73] "2020-02-12 IST" "2020-04-11 IST" "2020-01-21 IST" "2020-04-27 IST"
[77] "2020-03-31 IST" "2020-05-01 IST" "2020-05-11 IST" "2020-06-01 IST"
[81] "2020-04-01 IST" "2020-10-01 IST" "2020-02-12 IST" "2020-04-11 IST"
[85] "2020-01-21 IST" "2020-04-27 IST" "2020-03-31 IST" "2020-05-01 IST"
[89] "2020-05-11 IST" "2020-06-01 IST" "2020-04-01 IST" "2020-10-01 IST"
[93] "2020-02-12 IST" "2020-04-11 IST" "2020-01-21 IST" "2020-04-27 IST"
[97] "2020-03-31 IST" "2020-05-01 IST" "2020-05-11 IST" "2020-06-01 IST"
raja
Published on 21-Nov-2020 05:32:24
Advertisements