Reputation: 51
What I'm trying to do is to calculate the total days between two dates. If the dates are in "yyyy/mm/dd" format I did it this way:
EndDate <- "2012/02/01"
StartDate <- "1900/01/01"
DiffBewtweenDates <- data.frame(date=c(EndDate),start=c(StartDate))
DiffBewtweenDates$date_diff <- as.Date(as.character(DiffBewtweenDates$date), format="%Y/%m/%d")-
as.Date(as.character(DiffBewtweenDates$start), format="%Y/%m/%d")
DiffBewtweenDates
And it worked. But I'm requested to get at least the EndDate in this format "FullDayName, DayNumber of FullMonthName of FullYearNumber". Something like this "Sunday, 1 of February of 2012".
As I understand by the R Manual, it would be ...format="%A, %d of %B of %Y"
But it doesn't work and I can't figure out why.
Thanks in advance for any idea.
Upvotes: 0
Views: 144
Reputation: 2584
Simply to calculate difference in days and get desired output you can do
Sys.setlocale("LC_TIME", "C")
EndDate <- as.Date("2012/02/01", format = "%Y/%m/%d")
StartDate <- as.Date("1900/01/01", format = "%Y/%m/%d")
EndDate - StartDate
# Time difference of 40938 days
format(EndDate, "%A, %d of %B of %Y")
# [1] "Wednesday, 01 of February of 2012"
Upvotes: 1
Reputation: 54237
Perhaps you got to change your locale to english:
Sys.setlocale("LC_TIME", "english")
date <- "Sunday, 1 of February of 2012"
lubridate::guess_formats(date, orders = "dmy")
# dmy
# "%A, %d of %B of %Y"
as.Date(date, guess_formats(date, orders = "dmy"))
# [1] "2012-02-01"
Anyway, you can use lubridate's guess_formats
function to guess the formats for many date strings.
Upvotes: 1