简体   繁体   English

从日期中提取小时

[英]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. 但是我不确定如何处理星期几或​​+0000。 Thanks for the help! 谢谢您的帮助!

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: 您只需查看strptime的文档即可确定要使用的正确格式掩码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM