Search code examples
rdateanytime

Issue with the format in anytime package


library("anytime")

This works well:

anytime("08/24/2014 01:28:00")
anytime("2014/08/24 01:28:00")

[1] "2014-08-24 00:28:00 NZST"

This doesn't:

anytime("24/08/2014 01:28:00")
anytime("2014/24/08 01:28:00")

[1] NA

What is the reason for this and what would be the options (using anytime package ONLY)?


Solution

  • We can add the formats with addFormats in anytime if that formats are not in the default list of formats by using

    getFormats()
    #[1] "%Y-%m-%d %H:%M:%S%f"    "%Y/%m/%d %H:%M:%S%f"    "%Y%m%d %H%M%S%f"       
    #[4] "%Y%m%d %H:%M:%S%f"      "%m/%d/%Y %H:%M:%S%f"    "%m-%d-%Y %H:%M:%S%f"   
    #[7] "%Y-%b-%d %H:%M:%S%f"    "%Y/%b/%d %H:%M:%S%f"    "%Y%b%d %H%M%S%F"       
    #[10] "%Y%b%d %H:%M:%S%F"      "%b/%d/%Y %H:%M:%S%f"    "%b-%d-%Y %H:%M:%S%f"   
    #[13] "%d.%b.%Y %H:%M:%S%f"    "%Y-%B-%d %H:%M:%S%f"    "%Y/%B/%d %H:%M:%S%f"   
    #[16] "%Y%B%d %H%M%S%f"        "%Y%B%d %H:%M:%S%f"      "%B/%d/%Y %H:%M:%S%f"   
    #[19] "%B-%d-%Y %H:%M:%S%f"    "%d.%B.%Y %H:%M:%S%f"    "%a %b %d %H:%M:%S%F %Y"
    #[22] "%Y-%m-%d"               "%Y%m%d"                 "%m/%d/%Y"              
    #[25] "%m-%d-%Y"               "%Y-%b-%d"               "%Y%b%d"                
    #[28] "%b/%d/%Y"               "%b-%d-%Y"               "%Y-%B-%d"              
    #[31] "%Y%B%d"                 "%B/%d/%Y"               "%B-%d-%Y"   
    

    If we check the format that gives NA, it is not in the getFormats list

    c("%d/%m/%Y %H:%M:%S", "%Y/%d/%m %H:%M:%S") %in% getFormats()
    #[1] FALSE FALSE
    

    So, we can add the formats with addFormats and apply the anytime

    anytime::addFormats(c("%d/%m/%Y %H:%M:%S", "%Y/%d/%m %H:%M:%S")) 
    anytime("24/08/2014 01:28:00")
    #[1] "2014-08-24 01:28:00 IST"
    anytime("2014/24/08 01:28:00")
    #[1] "2014-08-24 01:28:00 IST"
    

    Update

    In the latest version of anytime, there are 41 formats in getFormats()

    length(getFormats())
    #[1] 41
    

    But, the format specified in the OP's post is still not included and would have to follow the addFormats route