使用R查找昨天的日期 [英] finding yesterday's date using R

查看:175
本文介绍了使用R查找昨天的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的R脚本之一中,我需要找到昨天的日期,我可以使用此命令在Mac上轻松完成此操作.

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

因此,Sys.Date()-1本质上是给我昨天的日期.但是,当我在ubuntu 16.04机器(AWS实例)上运行此命令时,它不起作用.而是返回今天的日期?还有其他人经历过吗?不使用Sys.Date()-1命令如何获取昨天的日期?

更新:Amazon AWS说该服务器位于加利福尼亚州北部,但是进一步检查看来,该服务器的时区实际上比美国时区早了7个小时?我使用了 IP位置查找器这个工具来尝试对IP地址进行地理位置定位,并在N.Califorina?令人困惑的是,但现在我只是将7小时添加到我的日期变量中,这是一个短期解决方案,但我想了解造成此时区问题的原因.

解决方案

Sys.Date返回"Date"类的对象,而Sys.time返回"DateTimeClasses"的"POSIXct"类的对象.

Sys.time将更容易处理精确的时间,因为"POSIXct"是历元后的秒数,而不是Date对象.请参阅: https://www.epochconverter.com/以获得对时代时间的更好理解.

无论如何,这两种方法都应该起作用,可能只是您的Ubuntu系统的时间不正确.

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.

解决方案

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.

这篇关于使用R查找昨天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆