任何给定年份如何获得相同的平日? [英] How to Get the Same Weekday Last Year Given any Given Year?

查看:112
本文介绍了任何给定年份如何获得相同的平日?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

去年,我想要去年一样。我如何最好在R中做到这一点。例如,在2010/01/03星期日,我想获得前一周的星期日。

 #星期日
工作日(as.Date(2010/01/03,format =%Y /%m /%d))

#星期六
平日(as.Date(2009/01/03,format =%Y /%m /%d))


解决方案

一年前找到相同的工作日,只需从给定日期减去52周或364天: / p>

  d<  -  as.Date(2010-01-03)
平日(d)
#[1]星期日
d - 52L * 7L
#[1]2009-01-04
平日(d - 52L * 7L)
#[1 ]星期日

请注意,日历年有365天(或闰年366天)是一两天超过52周。所以,一年前同一个工作日的日历日期移动了一两天。 (或者说,为什么新年除夕总是在不同的工作日。)


I would like to get the same day last year given any year. How can I best do this in R. For example, given Sunday 2010/01/03, I would like to obtain the Sunday of the same week the year before.

# "Sunday"
weekdays(as.Date("2010/01/03", format="%Y/%m/%d"))

# "Saturday"
weekdays(as.Date("2009/01/03", format="%Y/%m/%d"))

解决方案

To find the same weekday one year ago, simply subtract 52 weeks or 364 days from the given date:

d <- as.Date("2010-01-03")
weekdays(d)
#[1] "Sunday"
d - 52L * 7L
#[1] "2009-01-04"
weekdays(d - 52L * 7L)
#[1] "Sunday"

Please note that the calendar year has 365 days (or 366 days in a leap year) which is one or two days more than 52 weeks. So, the calendar date of the same weekday one year ago moves on by one or two days. (Or, it explains why New Year's Eve is always on a different weekday.)

这篇关于任何给定年份如何获得相同的平日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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