How convert Persian date to Gregorian in R?

Viewed 531

I want to convert a Persian calendar date (e.g. 1399/03/27) to a Gregorian calendar date in R. Is there any function to do so in R?

1 Answers

As @RuiBarradas pointed out in comments, we may use ConvCalendar::OtherDate. Since the package is no longer on CRAN we first need to install it from source:

# p.url <- "https://cran.r-project.org/src/contrib/Archive/ConvCalendar/ConvCalendar_1.2.tar.gz"
# install.packages(p.url,
#                  repos=NULL, type="source")

OtherDate wants day, month, and year separately in a vector and in this order. It creates a list-like object of class "OtherDate" that can be fed into as.Date to return Gregorian date.

library(ConvCalendar)
persian <- OtherDate(day=27, month=03, year=1399, calendar="persian")
gregorian <- as.Date(persian)
gregorian
# [1] "2020-06-16"

More automatically we may use do.call.

persian <- "1399/03/27"
persian <- do.call(OtherDate, c(as.list(rev(el(strsplit(persian, "/")))), 
                                calendar="persian"))
gregorian <- as.Date(persian)
gregorian
# [1] "2020-06-16"
Related