Transforming a long date string to a Date in R

后端 未结 2 1990
甜味超标
甜味超标 2021-01-14 05:14

I have a date variable that is formatted as:

25-APR-2013 03.05.03.000000000 PM

I have converted it to a character and tried to get it to fo

相关标签:
2条回答
  • 2021-01-14 05:37

    If you just want the date, then just specify that portion in the format argument:

    as.Date("25-APR-2013 03.05.03.000000000 PM","%d-%b-%Y")
    #[1] "2013-04-25"
    

    If you wanted the full date-time, use as.POSIXct with a custom format:

    as.POSIXct("25-APR-2013 03.05.03.000000000 PM",format="%d-%b-%Y %I.%M.%OS %p")
    #[1] "2013-04-25 15:05:03 CDT"
    
    0 讨论(0)
  • 2021-01-14 05:52

    Just extract the portion of the string containing the useful info with substr() and feed it to as.Date()

    orig.string<-"25-APR-2013 03.05.03.000000000 PM"
    as.Date(substr(orig.string, start=1, stop=11), format="%d-%b-%Y")
    
    0 讨论(0)
提交回复
热议问题