Zack Vlliet
Zack Vlliet

Reputation: 63

Extracting the hour from a date

I have a date in a semi-odd format. It's written like:

Tue Oct 11 20:56:33 +0000 2016

The date is a string. The date shows what time and day a tweet was sent as in a data set. I want to find what hour each of my tweets were sent out. What is the best way to work with this? Is there a way to convert it to a date type easily, or is there a way to work with that string with what I want to do? Obviously I'd like to something like:

format(strptime(testTweets$Date, format = "%m %d %H:%M%S %Y", "%H"))

But I'm not sure how to work with the day of the week or the +0000. Thanks for the help!

Upvotes: 1

Views: 56

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

The above comment beat me to it, but try the following code:

strptime(x, format = "%a %b %d %H:%M:%S %z %Y")

[1] "2016-10-11 20:56:33"

You only need to check the documentation for strptime to figure out the correct format mask to use:

https://www.rdocumentation.org/packages/base/versions/3.4.3/topics/strptime

Upvotes: 2

Related Questions