Zachary Donnini
Zachary Donnini

Reputation: 39

Drawing a Month From a Date

I'm wondering how I can draw the month and year from a string date in the form MM/DD/YYYY.

I've tried lubridate but it isn't working, nor is anything else.

Upvotes: 0

Views: 75

Answers (2)

JineshEP
JineshEP

Reputation: 748

library(lubridate)
library(dplyr)

date = "2012-10-03"
ymd(date)

ymd(date) %>% day()
[1] 3

ymd(date) %>% month()
[1] 10

For the format MM/DD/YYYY use:

date = "12/22/2013" 
mdy(date) %>% month()
[1] 12

Upvotes: 3

niko
niko

Reputation: 5281

You can use format

month <- format(as.Date('07/04/2010', format = '%m/%d/%Y'), '%m') # or %b or %B
year <- format(as.Date('07/04/2010', format = '%m/%d/%Y'), '%Y') # or %y

Upvotes: 0

Related Questions