Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Date print format from yyyy-mm-dd to dd-mm-yyyy

Tags:

date

r

I am trying to convert the format of the dates from yyyy-mm-dd to dd-mm-yyyy format. For example:

D <- "06.12.2012"  
D <- as.Date(D, "%d.%m.%Y")
D
# [1] "2012-12-06"

How can I convert it to "06-12-2012"?

like image 971
AliCivil Avatar asked Jan 03 '13 22:01

AliCivil


2 Answers

a) Parse

   d <- as.Date( "06.12.2012", "%d.%m.%Y")

b) Format

   strftime(d, "%m-%d-%Y")

or

   format(d, "%m-%d-%Y")
like image 50
Dirk Eddelbuettel Avatar answered Sep 17 '22 14:09

Dirk Eddelbuettel


Strictly speaking, you can't. A Date is an integer (days since 1970-01-01) that looks like a character string when printed. The printed format for Date objects is YYYY-MM-DD because that's the ISO-8601 standard.

As @GSee mentioned in chat, you would have to re-define the print.Date method with something like:

print.Date <- function(x, max=NULL, ...) {
  if (is.null(max)) 
    max <- getOption("max.print", 9999L)
  if (max < length(x)) {
    print(format(x[seq_len(max)], "%d-%m-%Y"), max = max, ...)
    cat(" [ reached getOption(\"max.print\") -- omitted", 
      length(x) - max, "entries ]\n")
  }
  else print(format(x, "%d-%m-%Y"), max = max, ...)
  invisible(x)
}
like image 34
Joshua Ulrich Avatar answered Sep 21 '22 14:09

Joshua Ulrich