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"?
a) Parse
d <- as.Date( "06.12.2012", "%d.%m.%Y")
b) Format
strftime(d, "%m-%d-%Y")
or
format(d, "%m-%d-%Y")
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With