Home > Blockchain >  How to Invert date format
How to Invert date format

Time:12-31

I have a large database with one of the columns containing dates with the following format: DD-MM-YYYY. I would like to invert the date format, to something like this: YYYY-MM-DD. Can someone tell me how can I do it using bash OR R?

CodePudding user response:

A possible solution:

library(tidyverse)
library(lubridate)

df <- data.frame(date=c("11-4-2021","5-6-2019"))

df %>% 
  mutate(date2 = dmy(date) %>% ymd)

#>        date      date2
#> 1 11-4-2021 2021-04-11
#> 2  5-6-2019 2019-06-05

CodePudding user response:

you can simply use datetime module:

str_time = datetime.strftime(date_time, '%Y-%m-%d')

CodePudding user response:

In bash, we can use string manipulation:

dmy=30-12-2021
echo "${dmy:6:4}-${dmy:3:2}-${dmy:0:2}"    # 2021-12-30

or with read:

IFS="-" read -r d m y <<<"$dmy"
echo "$y-$m-$d"

CodePudding user response:

I used R to solve my problem like this:

df > data.frame with dates on column "eventDate". Dates were in the format DD-MM-YYYY. There were several cells with incomplete dates (e.g. MM-YYYY or YYYY).

library(tidyr)
x <- separate(df, col = eventDate, into = c("day", "month", "year"), sep="-")
y <- x %>% unite("eventDate_2", year:month:day, remove=TRUE, sep="-", na.rm= TRUE)
y <- cbind(y, df$eventDate) # add the original column for comparing if it had worked and correct individual errors.
  • Related