finding yesterday's date using R

為{幸葍}努か 提交于 2019-12-30 08:59:06

问题


In one of my R scripts I need to find the date of yesterday, I can do that easily on my mac with this command.

yesterday <- format(Sys.Date()-1,"%m/%d/%Y")
yesterday
[1] "03/17/2017"

So essentially the Sys.Date()-1 gives me yesterday's date. However, when I run this command on my ubuntu 16.04 machine(AWS instance) it doesn't work. It returns today's date instead? Has anyone else experienced this? How could I get yesterday's date without using the Sys.Date()-1 command?

UPDATE: Amazon AWS says the server location is in N. California, however upon further inspection it seems that the server's timezone is in fact 7 hours ahead? I used this tool, IP Location Finder to try to geo-locate the IP address, and the IP was found in N.Califorina? Confusing but for now I'm just adding 7 hours to my date variable and it's a short term fix, but I would like to understand what's causing this timezone issue.


回答1:


Sys.Date returns an object of class "Date" while Sys.time returns an object of class "POSIXct" (of DateTimeClasses).

Sys.time will be much easier to work with for manipulating exact times, since "POSIXct" is the number of seconds post epoch as opposed to a Date object. See: https://www.epochconverter.com/ to get a better understanding of epoch time.

Regardless, either method should work and it might just be your Ubuntu system isn't on the correct time.



来源:https://stackoverflow.com/questions/42881954/finding-yesterdays-date-using-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!