161

I'm working with a csv which unfortunately has logged datetimes using the number format of 42705 although it should be 01/12/2016.

I'd like to convert it to the right format in R using lubridate or some other package. Is there a function that will handle it?

2
  • 2
    This might set you up. Commented Apr 5, 2017 at 11:56
  • Thank you for such a fast reply. Commented Apr 5, 2017 at 11:58

6 Answers 6

271

You don't need to use lubridate for this, the base function as.Date handles this type of conversion nicely. The trick is that you have to provide the origin, which in Excel is December 30, 1899.

as.Date(42705, origin = "1899-12-30")
# [1] "2016-12-01"

If you want to preserve your column types, you can try using the read_excel function from the readxl package. That lets you load an XLS or XLSX file with the number formatting preserved.

EDIT: Relevant XKCD

XKCD comic strip

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

12 Comments

Thank you for that. I'm still very new to R. It worked a treat.
Quite welcome. I'm editing my response now since I realized the origin is slightly off. Give me a minute before you use this code in your project :-)
In the French version of excel, the best solution is quite fascinating, for a date with hours: as_datetime(Date,origin = "1969-12-31 24:00:00")
For those who wonder where the 1899-12-30 came from and if it is correct, it seems in some Excel versions Microsoft has used 1900 as the reference date and in some versions 1904! for this reason I suggest you to confirm the date from some other source as well. Source: support.microsoft.com/en-us/office/…
upvote was for XKCD :-D
|
49

Here is another way to do it using janitor and tibble packages:

install.packages("janitor")
install.packages("tibble")

library(tibble)
library(janitor)

excel_numeric_to_date(as.numeric(as.character(YourDate)), date_system = "modern")    

1 Comment

convert_to_date() - also from the janitor package - can be used when there is a mix of Excel numeric dates and actual dates
25

openxlsx package also allows xls date conversion:

openxlsx::convertToDate(42705)
[1] "2016-12-01"

And as suggested by @Suren, convertToDateTime allows datetime conversion:

openxlsx::convertToDateTime(42705.5)
[1] "2016-12-01 12:00:00"

3 Comments

convertToDateTime if it is datetime.
For some reason, this function only converts to date, not datetime, even though I am providing decimal numbers in my data frame. The function(s) from "janitor" work well and I don't have the same problem with them.
@K bro, tested again above example, still converts to datetime on R 4.2
5

If you work with the data.table package you could use as.IDate() for that:

require(data.table)

as.IDate(42705, origin = "1899-12-30")
# [1] "2016-12-01"

Works like base::as.Date() here.

Comments

4

As it was said, very good options:

as.Date(42705, origin = "1899-12-30")

openxlsx::convertToDate(42705)

Another way also could be:

format(as.Date(as.Date("1899-12-30") + 42705, "%d-%m-%Y"), "%d-%m-%Y")

Note you can change the output format where it's written %d-%m-%Y

(first of all, convert as.numeric if it's imported as character!,or converting in the formula:

format(as.Date(as.Date("1899-12-30") + as.numeric( number formatted as character), "%d-%m-%Y"), "%d-%m-%Y")

Comments

2

In line with the Janitor solution provided by Reza, if you have a mix of Excel numeric dates and incorrectly formatted dmy dates, this will work:

df$Procedure.Date <- convert_to_date(df$Procedure.Date, character_fun = lubridate::dmy, string_conversion_failure = "warning")

The character_fun determines the format of date entries, the string_conversion_failure argument specifies that you'll get a warning but the conversion will proceed and saved in your data.

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.