s-news
[Top] [All Lists]

Re: Formatting dates

To: fisher@plessthan.com
Subject: Re: Formatting dates
From: sbackwards@comcast.net (SD Chasalow)
Date: Mon, 05 Nov 2007 19:27:38 +0000
Cc: s-news@lists.biostat.wustl.edu
Hi,

I use the following function for this sort of thing, in both S-Plus and R.  
Simple modifications would be required should you be so unwise as to desire a 
format other than the one that I prefer...

Cheers,
Scott

Scott Chasalow
Bristol-Myers Squibb

"ISOnow" <- 
function(zone = "")
{
        # DATE CREATED:  08 Mar 2005
        # AUTHOR:  Scott D. Chasalow
        #
        # DESCRIPTION:
        #       Returns a character string giving the current date and
        #       time in (approximately) ISO format, e.g.
        #       "2005-03-08 17:05:36 EST".
        #
        # OPTIONAL ARGUMENTS:
        # zone  character string specifying the time zone to use in
        #       the return value.  The default is the current time zone
        #       reported by your OS (which is NOT necessarily the same
        #       as the value of option time.zone, if any).
        #
        # VALUE:
        #       A character string giving the current date and time in
        #       (approximately) ISO format, e.g.
        #       "2005-03-08 17:05:36 EST".
        #
        # DETAILS:
        #       In R, this function uses system functions Sys.time and
        #       format.POSIXct.  In S-Plus, it uses functions date and
        #       timeDate.
        #
        #       This function can for most purposes replace functions
        #       now, today, and isoDateTime (which you quite likely
        #       do not have anyway).  isoDateTime, in particular, works
        #       fine in S-PLUS 6.2 under UNIX and Windows, and gives you
        #       the option of including time or not.  But it does not
        #       work in R, and is less efficient and pretty than this
        #       function.
        #
        # SEE ALSO:
        #      R:  Sys.time, format.POSIXct, strptime
        #      S-Plus:  date, timeDate, now, today, isoDateTime
        #
        if(isR()) return(format(Sys.time(), tz = zone, usetz = T))
        dat <- date()
        if(missing(zone) || zone == "")
                zone <- unpaste(dat, sep = " ")[[5]]
        out <- timeDate(dat, in.format = "%w %m %d %H:%M:%S %Z %Y", format = 
                "%Y-%02m-%02d %02H:%02M:%02S %Z", zone = zone)
        as(out, "character")
}

"isR" <- 
function()
{
        existsFunction("is.R") && is.R()
}



 > -------- Original Message --------
> Subject:      [S] Formatting dates
> Date:         Mon, 05 Nov 2007 07:31:25 -0800
> From:         Dennis Fisher <fisher@plessthan.com>
> To:   s-news@lists.biostat.wustl.edu
> 
> Colleagues
> 
> I am working with Splus 8.0 in RedHat Linux.  I am attempting to perform
> a task in Splus that is simple to do in R, reformatting the system time
> to a string (yymmdd-HHMMSS).
> 
> In R, the command is:
>   > format(Sys.time(), '%y%m%d-%H%M%S')
> [1] "071105-072756"
> 
> In Splus, date() returns: [1] "Mon Nov 05 07:29:46 PST 2007"
> but I cannot find an easy means to reformat it.
> 
> Any help will be appreciated.  Thanks.
> 
> Dennis
> 
> 
> 
> Dennis Fisher MD
> 
> P < (The "P Less Than" Company)
> 
> Phone: 1-866-PLessThan (1-866-753-7784)
> 
> Fax: 1-415-564-2220
> 
> www.PLessThan.com

<Prev in Thread] Current Thread [Next in Thread>