1

I have a date column in a dataframe in R with its usual format of yyyy-mm-dd and I am trying to turn in to a string with the format function

this is what I have so far

format(data_future$date, '%d/%m/%Y ')

but I get values like this "04/03/2021"

how do I remove the leading 0 from the day and month so the final result would look like this

"4/3/2021"

Thanks for the help

Juan Lozano
  • 635
  • 1
  • 6
  • 17

1 Answers1

0

You can write a custom format function of your own -

custom_format <- function(x1, format, ...) {
  gsub("0(?=\\d/)", "",format(x1, format, ...), perl = TRUE)  
}

custom_format(as.Date('2021-04-03') + 1:10, '%d/%m/%Y')

# [1] "4/4/2021"  "5/4/2021"  "6/4/2021"  "7/4/2021"  "8/4/2021"  "9/4/2021" 
# [7] "10/4/2021" "11/4/2021" "12/4/2021" "13/4/2021"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213