VOOZH about

URL: https://www.geeksforgeeks.org/r-language/how-to-separate-date-and-time-in-r/

⇱ How to separate date and time in R ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to separate date and time in R ?

Last Updated : 29 Jun, 2021

In this article, we are going to separate date and time in R Programming Language.  Date-time is in the format of date and time (YYYY/MM/DD HH:MM:SS- year/month/day Hours:Minute:Seconds). 

Extracting date from timestamp: We are going to extract date by using as.Date() function.

Syntax:

 as.Date(data)

where data is the time stamp.

Extracting time from the time stamp: We can do this by using as.POSIXct() function. To get a particular time hour format we can use the format() function

Syntax:

format(as.POSIXct(data), format = "%H:%M")

Where,

  • as.POSIXct() is used to extract time from the time stamp
  • format is used to get the time format. Ex : hours:Minutes and seconds
  • data is the time stamp

Example 1:

Output:

👁 Image

Example 2: 

Output:

👁 Image

Example 3:

Output:

👁 Image
Comment
Article Tags:

Explore