Skip to content

Default Format for Dates?

5 messages · khobson@fd9ns01.okladot.state.ok.us, Roger D. Peng, Gabor Grothendieck

#
Is there anyway to preset date formats?  I have a date from a cover.dbf
that is shown as this:
[1] "2003-06-24"

The numeric value in cover$FINALREPOR is 12227.  I'd rather not create
another vector to hold the properly formatted date.

When I put this in a WordPerfect merge, I want the date to be June 24,
2003.   I could take care of the problem in a WordPerfect macro but I'd
rather do it as an R default date format if possible.

I researched some of this in the archives, FAQ's and manuals but didn't
find anything that met my need.  I would imagine that this type of question
is probably an FAQ but I didn't find it.

mailto:khobson at odot.org
Kenneth Ray Hobson, P.E.
Oklahoma DOT - QA & IAS Manager
200 N.E. 21st Street
Oklahoma City, OK  73105-3204
(405) 522-4985, (405) 522-0552 fax

Visit our website at:
http://www.okladot.state.ok.us/materials/materials.htm
#
On 6/10/05, khobson at fd9ns01.okladot.state.ok.us
<khobson at fd9ns01.okladot.state.ok.us> wrote:
I am not entirely sure I understand what you are asking but I assume
you want a Date variable such that print, format and as.character,
when applied to it, produce default output of a prespecified format.

Although I do not think Date will do that, chron can
associate a default format with a chron variable.

library(chron)

# custom format for a dates (i.e. chron) object 
my.format <- function(x) format(as.Date(dates(x)), "%B %d %Y")

# test data
my.Date <- Sys.Date() + 0:9

# convert to chron and associate my.format to it
my.chron <- chron(unclass(my.Date), out.format = my.format)

print(my.chron)
my.chron
as.character(my.chron)
format(my.chron)

Note that you can find more information on chron and various
conversions in my article in RNews 4/1 and especially the table 
at the end of that article.
#
Is something like this what you want?

x <- as.Date("2003-06-24")
format(x, "%B %d %Y")

or perhaps

as.character(x, "%B %d %Y")

-roger
khobson at fd9ns01.okladot.state.ok.us wrote:

  
    
#
On 6/10/05, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
Here is a different solution that does not involve chron.  In this one
we define a subclass of Date called mdy that has a default
format as you specified implemented by defining print, as.character
and format methods for it:

# define mdy methods
print.mdy <- print.Date
as.character.mdy <- format.mdy <- function(x, format = "%B %d %Y")
	format(structure(x, class = "Date"), format = format)

# test data of class mdy (which is a subclass of Date)
x <- Sys.Date() + 0:9 
class(x) <- c("mdy", "Date")

# test out print, format and as.character methods for class mdy
print(x)
format(x)
as.character(x)
x
#
On 6/10/05, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
Actually there was no point in defining print.mdy and as.character.mdy
as further examination shows they both operate through format so the 
above can be simplified to just:


format.mdy <- function(x, format = "%B %d %Y")
       format(structure(x, class = "Date"), format = format)

# test data of class mdy (which is a subclass of Date)
x <- Sys.Date() + 0:9
class(x) <- c("mdy", "Date")

# test out print, format and as.character methods for class mdy
print(x)
format(x)
as.character(x)
x