- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert a date or date vector to POSIXct in R?
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
> date1<-"2020-04-01" > as.POSIXct(date1,format="%Y-%m-%d")
Output
[1] "2020-04-01 IST"
Example2
> 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
> 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"
- Related Articles
- How to create a date vector with randomization in R?
- Create a date column in R using a date vector excluding weekends.
- How to convert a date to quarter and year in R?
- How to convert a MySQL date to JavaScript date?
- How to convert a Python date string to a date object?
- How to convert epoch Date to meaningful JavaScript date?
- Convert date parts to date in MongoDB
- How to Convert Date Stored as Text to Date in Excel?
- How to correctly convert a date format into a MySQL date?
- Convert UK DATE to MySQL date?
- How to convert a date format in MySQL?
- How to Convert Numbers to Year/Month/Day or Date in Excel?
- How to Convert Week Number to Date or Vice Versa in Excel?
- How to convert a string to date in MySQL?
- How to convert a data frame column to date that contains integer values in R?

Advertisements