Home > Software engineering >  Issue with date formatting in R
Issue with date formatting in R

Time:04-06

I'm trying to format the date and having issues using the following: d <- format(Sys.time(), "%m/%d/%Y") sets d = 04/05/2022 as I'd expect.

When I convert d to a date using the following: d <- as.Date(d) That does convert it to a date datatype however the value becomes 4-05-20

Any help would be greatly appreciated.

CodePudding user response:

You can set a format in the as.Date function as well. The thing is that the order format is different from your system time, Sys.Date, and it uses - instead of /:

d <- format(Sys.Date(), "%m/%d/%Y")
d

Output:

[1] "04/05/2022"

When using as.Date:

as.Date(d, format =  "%m/%d/%Y")

Output:

[1] "2022-04-05"

As you can see it is actually the same but with different order and notation.

  •  Tags:  
  • r
  • Related