Learn R Programming

RSurvey (version 0.7-5)

POSIXct2Character: Convert POSIXct to Character

Description

Convert objects from POSIXct to character class.

Usage

POSIXct2Character(obj, fmt = "%Y-%m-%d %H:%M:%OS3")

Arguments

obj
POSIXct; vector of calendar dates and times.
fmt
character; the conversion specification format.

Value

  • Returns a character vector representing time.

References

A detailed explanation of the problem is provided http://stackoverflow.com/questions/7726034/how-r-formats-posixct-with-fractional-seconds{here}; solution provided http://stackoverflow.com/questions/15383057/accurately-converting-from-character-posixct-character-with-sub-millisecond-da{here}.

See Also

strptime

Examples

Run this code
val <- c("11/10/2011 07:49:36.3",
         "04/01/2013 17:22:08.139",
         "01/06/2013 01:02:16.333",
         "12/14/2038 15:42:04.577895")
date.time <- as.POSIXct(val, format="%m/%d/%Y %H:%M:%OS")

options("digits.secs" = 3)
format(date.time, fmt = "%d/%m/%Y %H:%M:%OS")
format(date.time, fmt = "%d/%m/%Y %H:%M:%OS3")

POSIXct2Character(date.time, fmt = "%d/%m/%Y %H:%M:%OS3")
POSIXct2Character(date.time, fmt = "%d/%m/%Y %H:%M:%OS4")
POSIXct2Character(date.time, fmt = "%d/%m/%Y %H:%M:%OS2")

POSIXct2Character(date.time, fmt = "%H:%M:%OS3 %Y-%m-%d")

Run the code above in your browser using DataLab