Search code examples
rcalendartransform

Transform Persian (Jalali Calendar) dates to Gregorian dates


How can one automatically transform Persian dates (Jalali/Iranian calendar) to the dates according to the Gregorian calendar in R?

Suppose the data format is organized in months/day (for example "1.02") without a year.


Solution

  • We have to fill in something for the year in order to use the existing machinery. The current year in Jalali is apparently 1401: greg2jal(Sys.Date()) is (1401,3,4). So something like

    mdvec <- c(1.02, 2.03, 3.21)
    ## separate decimals into month/day
    month <- mdvec %/% 1
    day <- (mdvec %% 1) * 100
    cur_yr <- 1401
    year <- rep(cur_yr, length(mdvec))
    ## convert
    library(jalcal)
    do.call(c, Map(jal2greg, year, month, day))
    ## [1] "2022-03-22" "2022-04-22" "2022-06-10"
    

    warning: I don't know if there's a perfect one-to-one mapping between Jalali month/day and Gregorian month/day ... ?