如何从日期中减去/添加天数?
我正在尝试构建文件夹来存储数据拉取。我想用拉取数据的日期来标记文件夹。
前任。我从 mysql 中提取 5 天前的数据,我想将文件夹命名为 5 天前的日期。
MySQL可以轻松处理日期运算。我不确定 R 到底是如何做到的。我应该在 POSIXct 中减去适当的秒数,然后转换为 POSIXlt 以将文件夹命名为 MM_DD_YYYY 吗?
或者有更好的方法吗?
I'm trying to build folders to store data pulls. I want to label the folders with the day of that data in the pull.
Ex. I pull 5 days ago data from mysql i want to name the folder the date from 5 days ago.
MySQL can easily handle date arithmetic. I'm not sure exactly how R does it. Should i just subtract the appropriate number of seconds in POSIXct and then convert to POSIXlt to name the folder MM_DD_YYYY?
Or is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需减去一个数字:
由于
Date
类只有天,因此您可以对其进行基本算术运算。如果你出于某种原因想使用 POSIXlt,那么你可以使用它的插槽:
Just subtract a number:
Since the
Date
class only has days, you can just do basic arithmetic on it.If you want to use POSIXlt for some reason, then you can use it's slots:
答案可能取决于您的日期采用的格式,但这里是使用
Date
类的示例:您甚至可以使用不同的单位,例如周。
The answer probably depends on what format your date is in, but here is an example using the
Date
class:You can even play with different units like weeks.
当然有一个
lubridate
解决方案:相同:
与其他时间格式
There is of course a
lubridate
solution for this:is the same as
Other time formats could be: