0

I have imported some data from Excel containing a date and time cell. When I try to convert the data into a date and time format I only get very strange results. If I go for only date there is no problem and I get the correct date. (I am working in MacOS)

date <- 40259.93645833333

as.POSIXlt(date,origin='1904-01-01 00:00:00')
"1904-01-01 12:10:59 CET"
as.POSIXlt(date,origin='1970-01-01 00:00:00')
"1970-01-01 12:10:59 CET"
as.POSIXlt(date,origin='2010-01-01 00:00:00')
2010-01-01 12:10:59 CET"
as.Date(date,origin='1904-01-01')
"2014-03-23" #Correct date

I would like to know how to proceed and keep the time as well.

4
  • 1
    What is the origin in your excel? What is the first year? Commented Mar 18, 2014 at 8:13
  • The date specified is the first date in my Excel file. Commented Mar 18, 2014 at 8:15
  • 1
    Did you tried XLConnect? It should import data correctly (also with dates). Commented Mar 18, 2014 at 8:16
  • I imported my data using xlsx. Commented Mar 18, 2014 at 8:21

1 Answer 1

5

The number passed to as.POSIXlt are seconds since the origin:

as.POSIXlt(date*24*3600,origin='1904-01-01 00:00:00')
[1] "2014-03-23 23:28:29 CET"

Also, generic advice recommends using POSIXct instead of POSIXlt.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.