-2

How do I change the format of the date object from "%Y/%m/%d" to "%d/%m/%Y". I have tried the below-mentioned code without much success.

as.Date(bank$date,format="%d/%m/%Y")
      [1] "2007-11-02" "2007-11-09" "2007-11-16" "2007-11-23" "2007-11-30" "2007-12-07"
      [7] "2007-12-14" "2007-12-20" "2007-12-28" "2008-01-04" "2008-01-11" "2008-01-18"
     [13] "2008-01-25" "2008-02-01" "2008-02-08" "2008-02-15" "2008-02-22" "2008-02-29"

Thank you for your help.

  • 2
    Date class has a fixed format in R which is `YYYY-MM-DD`. Any other format will be character. – Ronak Shah Jul 01 '21 at 06:27
  • Thank you @Ronak Shah for the clarification. My issue is I am using the package `SystemicR` to calculate covar and getting this error message - 'Error in as.Date.default(df_data_returns[2:Number_Observations, 1], "%d/%m/%Y") : do not know how to convert 'df_data_returns[2:Number_Observations, 1]' to class “Date”. Can you help with the error msg. Thanks. – learning_r Jul 01 '21 at 06:46
  • which function in `SystemicR` are you using? – Onyambu Jul 01 '21 at 07:16
  • Maybe `SystemicR` is not expecting a `Date` object but a `character` with the format `%d/%m/%Y` ? – GKi Jul 01 '21 at 10:39

1 Answers1

1
date <- as.Date("2007-11-02")
new_date <- format(date, "%d/%m/%Y")
new_date
#> [1] "02/11/2007"

Created on 2021-07-01 by the reprex package (v2.0.0)

Desmond
  • 1,047
  • 7
  • 14
  • Thanks, @Desmond. The `new_date` is now a `character`. I want the `new_date` to be the `date' class object. – learning_r Jul 01 '21 at 06:25
  • 1
    that's simply not possible -- perhaps you can elaborate on why you think you need a date object in this format? – MichaelChirico Jul 01 '21 at 06:28
  • @MichaelChirico, I am using the package `SystemicR` to calculate covar and getting this error message - 'Error in as.Date.default(df_data_returns[2:Number_Observations, 1], "%d/%m/%Y") : do not know how to convert 'df_data_returns[2:Number_Observations, 1]' to class “Date”'. Thanks. – learning_r Jul 01 '21 at 06:44
  • 1
    You'll need to post a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with the data, libraries, your code and the error you're getting . – Desmond Jul 01 '21 at 06:54