Separating Date from the variable

In this dataset I have the start time and the end time. I want only the date to appear. Like for the first observation, I want 11th December in the date format (like 11-12-2021). Is this possible?

tibble::tribble(
                                                ~start_time,                                                 ~end_time,
  "Sat Dec 11 2021 08:14:27 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:47:09 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 09:26:19 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:56:07 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 09:36:19 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 10:25:33 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 08:30:09 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:53:43 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 08:25:23 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:33:19 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 08:29:13 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 10:05:44 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 08:20:24 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:46:08 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 08:22:23 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 10:33:01 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 08:29:46 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:44:33 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 08:17:37 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:44:22 GMT+0530 (India Standard Time)",
  "Fri Dec 10 2021 18:45:26 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:44:05 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 08:24:16 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:31:56 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 09:32:34 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 10:33:51 GMT+0530 (India Standard Time)"
  )
#> # A tibble: 13 x 2
#>    start_time                                              end_time             
#>    <chr>                                                   <chr>                
#>  1 Sat Dec 11 2021 08:14:27 GMT+0530 (India Standard Time) Sat Dec 11 2021 09:4~
#>  2 Sat Dec 11 2021 09:26:19 GMT+0530 (India Standard Time) Sat Dec 11 2021 09:5~
#>  3 Sat Dec 11 2021 09:36:19 GMT+0530 (India Standard Time) Sat Dec 11 2021 10:2~
#>  4 Sat Dec 11 2021 08:30:09 GMT+0530 (India Standard Time) Sat Dec 11 2021 09:5~
#>  5 Sat Dec 11 2021 08:25:23 GMT+0530 (India Standard Time) Sat Dec 11 2021 09:3~
#>  6 Sat Dec 11 2021 08:29:13 GMT+0530 (India Standard Time) Sat Dec 11 2021 10:0~
#>  7 Sat Dec 11 2021 08:20:24 GMT+0530 (India Standard Time) Sat Dec 11 2021 09:4~
#>  8 Sat Dec 11 2021 08:22:23 GMT+0530 (India Standard Time) Sat Dec 11 2021 10:3~
#>  9 Sat Dec 11 2021 08:29:46 GMT+0530 (India Standard Time) Sat Dec 11 2021 09:4~
#> 10 Sat Dec 11 2021 08:17:37 GMT+0530 (India Standard Time) Sat Dec 11 2021 09:4~
#> 11 Fri Dec 10 2021 18:45:26 GMT+0530 (India Standard Time) Sat Dec 11 2021 09:4~
#> 12 Sat Dec 11 2021 08:24:16 GMT+0530 (India Standard Time) Sat Dec 11 2021 09:3~
#> 13 Sat Dec 11 2021 09:32:34 GMT+0530 (India Standard Time) Sat Dec 11 2021 10:3~

Created on 2022-01-11 by the reprex package (v2.0.1)

Hello,

You can simply use the date formatting options to convert the string into a date like this:

library(tidyverse)

myData = tibble::tribble(
  ~start_time,                                                 ~end_time,
  "Sat Dec 11 2021 08:14:27 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:47:09 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 09:26:19 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:56:07 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 09:36:19 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 10:25:33 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 08:30:09 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:53:43 GMT+0530 (India Standard Time)",
  "Sat Dec 11 2021 08:25:23 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:33:19 GMT+0530 (India Standard Time)"
)

#BASE R
myData$start_time = as.Date(myData$start_time , format = "%a %b %d %Y")
myData$end_time = as.Date(myData$end_time , format = "%a %b %d %Y")


#Tidyverse
myData = myData %>% mutate(
  start_time = as.Date(start_time , format = "%a %b %d %Y"),
  end_time = as.Date(end_time , format = "%a %b %d %Y")
)

myData
#> # A tibble: 5 x 2
#>   start_time end_time  
#>   <date>     <date>    
#> 1 2021-12-11 2021-12-11
#> 2 2021-12-11 2021-12-11
#> 3 2021-12-11 2021-12-11
#> 4 2021-12-11 2021-12-11
#> 5 2021-12-11 2021-12-11

Created on 2022-01-11 by the reprex package (v2.0.1)

1 Like

You can either have a variable in a format that R interprets as a date and allows calculations, which will look like YYYY-MM-DD, or you can format it to look the way you want as a character string. @pieterjanvc shows how to convert your data to the first option. To add a column showing it as DD-MM-YYYY, you could take his output and add myData$start_format = format(myData$start_time, "%d-%m-%Y") and the equivalent for end date. That will create a character string which has the appearance you want, but which can't be calculated upon directly.

1 Like

Thanks for this. This works fine.

This will help. I hope it will serve your purpose

library(lubridate)

ts_data <- 
  tibble::tribble(
    ~start_time,                                                 ~end_time,
    "Sat Dec 11 2021 08:14:27 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:47:09 GMT+0530 (India Standard Time)",
    "Sat Dec 11 2021 09:26:19 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:56:07 GMT+0530 (India Standard Time)",
    "Sat Dec 11 2021 09:36:19 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 10:25:33 GMT+0530 (India Standard Time)",
    "Sat Dec 11 2021 08:30:09 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:53:43 GMT+0530 (India Standard Time)",
    "Sat Dec 11 2021 08:25:23 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:33:19 GMT+0530 (India Standard Time)",
    "Sat Dec 11 2021 08:29:13 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 10:05:44 GMT+0530 (India Standard Time)",
    "Sat Dec 11 2021 08:20:24 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:46:08 GMT+0530 (India Standard Time)",
    "Sat Dec 11 2021 08:22:23 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 10:33:01 GMT+0530 (India Standard Time)",
    "Sat Dec 11 2021 08:29:46 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:44:33 GMT+0530 (India Standard Time)",
    "Sat Dec 11 2021 08:17:37 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:44:22 GMT+0530 (India Standard Time)",
    "Fri Dec 10 2021 18:45:26 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:44:05 GMT+0530 (India Standard Time)",
    "Sat Dec 11 2021 08:24:16 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 09:31:56 GMT+0530 (India Standard Time)",
    "Sat Dec 11 2021 09:32:34 GMT+0530 (India Standard Time)", "Sat Dec 11 2021 10:33:51 GMT+0530 (India Standard Time)"
  )

ts_data

new_ts_data <- ts_data %>% 
  mutate(across(everything(), ~as_date(., format = '%a %b %d %Y')))
new_ts_data


Oh this is exactly what I wanted. Thanks a lot.