56361

Date Conversion from yyyy-mm-dd to dd-mm-yyyy

I am trying to convert the dates in my data frame from yyyy-mm-dd to dd-mm-yyyy format. for example:

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

returns :

"2012-12-06"

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

Answer1:

a) Parse

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

b) Format

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

or

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

Answer2:

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) }

Recommend

  • Twilio: programmatically join conference and play command or sound file?
  • Copy current CLOB column to new BLOB column in Oracle
  • Output Ruby duration in iso8601
  • Implicit declaration of scandir; alphasort is undeclared
  • (2 - 4 = -1) when int value assigned to pointer in C?
  • Keep playing a sound over and over again in Matlab?
  • Rails: Correct Way of Using the method of PostController in the Index file
  • Postback using javascript
  • Convert ctime to unicode and unicode to ctime python
  • Determine if a weekday is between two dates in R
  • PyRun_String stop sending result to stdout after any error
  • Catching exceptions caused in different threads [duplicate]
  • Why is new Number(8) not exactly equal to 8?
  • getFinancials (quantmod) and tq_get (tidy quant) not working?
  • Sending rails errors to rspec output
  • Why doesnt this Java loop in a thread work?
  • Need code translation from VB to C#
  • In Java, how can I construct a File from a resource?
  • XSLT foreach repeating nodes to flat
  • Do query loads all the data in memory
  • How to access culture data in globalize.js V1.0.0
  • Z3: Convert between FP and BitVector?
  • Debug.DrawLine not showing in the GameView
  • print() is showing quotation marks in results
  • CSS Linear-gradient formatting issue accross different browsers
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Master page gives error
  • Counter field in MS Access, how to generate?
  • Knitr HTML Loop - Some HTML output, some R output
  • Convert array of 8 bytes to signed long in C++
  • Font Awesome Showing Box instead of Icons
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • Can't mass-assign protected attributes when import data from csv file
  • sending mail using smtp is too slow
  • Sorting a 2D array using the second column C++
  • Python/Django TangoWithDjango Models and Databases