Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert numeric to date

I'd like to convert this type of numeric values to a date but it doesn't work.

20100727 for instance

I tried to convert the numeric to character and applied this :

as.Date("20100727", "Y%d%m")

but it doesn't work.

How can I do ?

like image 818
Ricol Avatar asked Apr 22 '13 13:04

Ricol


People also ask

How do I convert a number to date text?

Convert Excel date to text via NotepadSelect all of the dates you want to convert and press Ctrl+C to copy them. Open Notepad or any other text editor, and paste the copied dates there. Notepad automatically converts the dates to the text format. Press Ctrl+A to select all text strings, and then Ctrl+C to copy them.

How do I convert 8 digits to dates in Excel?

On the Data tab of the ribbon, click Text to Columns. Click Next >, then Next > again. Under 'Column data format', select Date, then select YMD from the drop-down next to the Date option button. Click Finish.

How do I convert a number to a short date in Excel?

Home Number Formatting ToolsClick Home tab, then click the drop-down menu in Number Format Tools. Step 2. Select Short Date from the drop-down list. The date is instantly displayed in short date format m/d/yyyy.


2 Answers

You were setting wrong order of month and date values (in your code was Year, Date, Month, should be Year, Month, Date).

as.Date("20100727", "%Y%m%d")
[1] "2010-07-27"
like image 126
Didzis Elferts Avatar answered Sep 29 '22 10:09

Didzis Elferts


If it is a numeric value, 20100727, as.Date requires it to be first converted to string, or else, would have issue

as.Date(20100727, "%Y%m%d")

Error in charToDate(x) : character string is not in a standard unambiguous format


anydate from anytime can internally do this conversion

library(anytime)
date1 <- anydate(20100727)
date1
#[1] "2010-07-27"
str(date1)
#Date[1:1], format: "2010-07-27"

Also, takes the character string as input

anydate("20100727")
#[1] "2010-07-27"
like image 36
akrun Avatar answered Sep 29 '22 10:09

akrun