-1

I am trying to format a date from a character of form mmm dd, yyyy

I tried :

date1 <- "Dec 05, 2016"
date2 <- format(date1, format="%d %B %Y")
 class(date2)
 date2

but this does not work. Thank you for any help.

I then tried as suggested:

date1 <- "Dec 05, 2016"
date2 <- format(as.Date(date1, "%b %d, %Y"), "%d %B %Y")
date2  <- as.Date(date2)

class(date2)
date2

but this still did not covert to the class date but was class: character

This seems to work ref:Convert character to Date in R

library(lubridate)
date1 <- "Dec 05, 2016"
date2 <- mdy(date1)
class(date2)
date2
Community
  • 1
  • 1
adam.888
  • 7,686
  • 17
  • 70
  • 105

1 Answers1

3

We can convert to 'Date' class and then convert to the format of interest

format(as.Date(date1, "%b %d, %Y"), "%d %B %Y")
#[1] "05 December 2016"
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks, that helps but unfortunately I still get it as character class.date1 <- "Dec 05, 2016" date2 <- format(as.Date(date1, "%b %d, %Y"), "%d %B %Y") date2 <- as.Date(date2) class(date2) date2 – adam.888 Dec 14 '16 at 09:52
  • I typed this above as it is not clear in the comment box – adam.888 Dec 14 '16 at 09:53
  • @adam.888 The `format` changes it to `character` class and `Date` class have only a single format i.e. `%Y-%m-%d` by design – akrun Dec 14 '16 at 09:55