Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having problems parsing string to datetime

Tags:

datetime

r

I'm having some problems parsing a string to datetime.

This is what I'm doing

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 are happening here:

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

So, the specific question is: How to correctly parse this string to datetime?

like image 377
Barranka Avatar asked Jan 10 '23 09:01

Barranka


1 Answers

(1) try strptime instead of strftime; I'm not sure what strftime does, but maybe not what you think.

(2) I don't think "p.m." will work at all; you might need some judicious gsub("p.m.","PM",...) use.

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
like image 129
Ben Bolker Avatar answered Jan 12 '23 09:01

Ben Bolker