Home > Software engineering >  Converting No Space date formatted as character to date
Converting No Space date formatted as character to date

Time:10-06

I have a date column where each date is written without spaces and is in character format.

Date
<chr>
"20130402"
"20130403"
"20130404"

I want to convert these values to date, but it does not work.

I tried the following code to do so:

dataframe %>%
 as.Date(Date, origin = "2013-04-02") 

However, this resulted in wrong dates, such as "57128-06-03". Can someone help me on how can I fix this?

CodePudding user response:

Assuming the timezone is UTC and the format is year,month,day:

strptime("20130402", "%Y%m%d", tz = "UCT")

CodePudding user response:

An option with ymd

library(dplyr)
library(lubridate)
dataframe <- dataframe %>%
    mutate(Date = ymd(Date))
  • Related