how to get the date format?

Is there a function that returns the date format?
For example:

x <- "06/23/2013 03:45:23"
formatDate (x)
"%m /%d /%Y  %H:%M:%S"

Is this what you're looking for?

x <- "06/23/2013 03:45:23"
x_posix <- as.POSIXct(x, format = "%m/%d/%Y %H:%M:%S")
x_posix
#> [1] "2013-06-23 03:45:23 EDT"
class(x_posix)
#> [1] "POSIXct" "POSIXt"

Created on 2020-04-28 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

I'm sorry, but I don't think I understand the question. Are you asking if there is a function that will guess the format of a character string? anytime() does a pretty good job of parsing character inputs:

x <- "06/23/2013 03:45:23"
anytime::anytime(x)
#> [1] "2013-06-23 03:45:23 EDT"

Created on 2020-04-28 by the reprex package (v0.3.0)

what I'm looking for is to get the character format:

x <- "06/23/2013 03:45:23"
fDate<-formatDate (x)
**fDate**
"%m /%d /%Y  %H:%M:%S"
that allows!
x <- "06/23/2013 03:45:23"
x_posix <- as.POSIXct(x, format = **fDate**)

But in your example, you will still need a parser to "guess" the format of you string, correct? I'm not sure if there is an R function that does this, but I would recommend reading more about anytime as well as Boost.Date_Time which is what anytime uses to do the string > date conversions.

I try to explain better
I want to know if there is a function that returns the format of a date:

examples

input date 			function		output format
"06:02:36"		          F(...)		"%H:%M:%S"
"06/23/2013 03:45:23"             F(...)		"%m/%d/%Y %H:%M:%S"
"01APR2008:09:00:00"              F(...)                "%d%b%Y:%H:%M:%S"
"23/06/2013 03:45:23"             F(...)		"%d/%m/%Y %H:%M:%S"
"1/05/2015 0:30"		  F(...)                "%d/%m/%Y %H:%M" 
"20140101"                        F(...)                "%Y%m%d"