Problems with parsing a string in datetime

I am having trouble parsing a string in datetime.

This is what I do

strftime("28/10/2014 09:05:55 p.m.", format='%d/%m/%Y %I:%M:%S %p')
##[1] "20/10/28 12:00:00 "

As you can see, three undesirable things happen here:

  • The returned date is incorrect!
  • The time is always set to 12:00:00
  • The return value is a String, not a datetime (this is completely irrelevant ... I can convert it to datetime later)

So, a specific question: how to parse this string in datetime correctly?

+4
source share
2 answers

(1) try strptimeinstead strftime; I'm not sure what it does strftime, but maybe not what you think.

(2) I do not think that "will work at all; you may need to use it wisely gsub("p.m.","PM",...).

strptime("28/10/2014 09:05:55 PM", format='%d/%m/%Y %I:%M:%S %p')
## [1] "2014-10-28 21:05:55 EDT"
strptime("28/10/2014 09:05:55 p.m.", format='%d/%m/%Y %I:%M:%S %p')
## NA
+6

, p.m. PM ( strptime, :

> strptime(sub("p\\.m\\.", "PM", "28/10/2014 09:05:55 p.m."), format='%d/%m/%Y %I:%M:%S %p')
[1] "2014-10-28 21:05:55 PDT"

, , , a.m. AM.

+3

All Articles