0

RStudio Version 1.0.136 Windows Ver: Windows10 Pro X64

I input a time with quotes, and want to try the strptime function. I have tried a lot of formats, but always get NA returned, why is that?

t3 <- "October 17, 1986 08:24"

strptime(t3, "%M %d, %Y %H:%M")

strptime(t3, "%B %d, %Y %H:%M")

strptime(t3, "%B %d %Y %H:%M")

strptime(t3, "%M %d %Y %H:%M")

strptime(t3, "%M-%d-%Y %H:%M")

strptime(t3, "%B-%d-%Y %H:%M")
Ryan
  • 55
  • 9
  • What is the result of `Sys.getlocale(category = "LC_ALL")`? – HubertL Jun 02 '17 at 22:36
  • 1
    the formats are not random, why are you trying so many combinations? The second one is correct by the way, this is like a multiple choice SAT question – rawr Jun 02 '17 at 22:44
  • I think I find the problem. I tried those in the RStudio, which gets me NA returned. So RStudio does not support this function? I used to think they would give me the same result. And by the way, would you please tell me how can I decide whether the month should be used by "B" or "M"? thanks a lot – Ryan Jun 03 '17 at 00:18
  • see documentation at https://stat.ethz.ch/R-manual/R-devel/library/base/html/strptime.html – Ajay Ohri Jun 03 '17 at 03:24

3 Answers3

1

As noted by rawr, your second attempt is correct:

t3 <- "October 17, 1986 08:24"

t <- strptime(t3, "%B %d, %Y %H:%M")

str(t)

Results:

POSIXlt[1:1], format: "1986-10-17 08:24:00"
PhilC
  • 767
  • 3
  • 8
  • @rawr I really cannot get the result with this function. Here's the screenshot on my computer, with exactly the same code.[Screen Shot](https://dl.dropboxusercontent.com/u/1174148/R.JPG). – Ryan Jun 03 '17 at 04:53
1

I find another thread which gives me a solution. Because my system language is not English, it cannot recognize "October". After I change it to "10", the code runs perfectly. Thanks for all the help.

Ryan
  • 55
  • 9
0
strptime(t3,"%B %d, %Y  %H:%M")

from https://stat.ethz.ch/R-manual/R-devel/library/base/html/strptime.html

Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60