Skip to content
Prev 388976 / 398506 Next

Converting characters back to Date and Time

Hello,


I'm assuming you're reading from an "*.xlsx" file. I'm not sure which
package you're using for this scenario, but my preference is 'openxlsx'. If
this is the package you're using, you could set argument 'detectDates' to
'TRUE', and then it should read them correctly.


FILE <- tempfile(fileext = ".xlsx")


openxlsx::write.xlsx(
    data.frame(V1 = as.Date("2000-01-01")),
    FILE
)


openxlsx::read.xlsx(FILE)
openxlsx::read.xlsx(FILE, detectDates = TRUE)


unlink(FILE)


The first one should read the dates as numbers (days since 1900-01-01 or
1904-01-01, depending upon setup), while the second should parse them to
class "Date". I hope this helps!
On Tue, Aug 31, 2021 at 4:26 PM Eliza Botto <eliza_botto at outlook.com> wrote:

            

  
  
Message-ID: <CAPcHnpRRCs1LjU6=YB=VamtKw1MPEvFXypvpEPFva3v6i4_Bjw@mail.gmail.com>
In-Reply-To: <AS8P194MB099979E9FD39C8A3705261579ACC9@AS8P194MB0999.EURP194.PROD.OUTLOOK.COM>