Search code examples
rweekday

Does R's 'weekdays' function get the date wrong?


I'm wondering if there is something wrong with my system or with the code I am using, but I seem to get the wrong answer from weekdays.
Can someone please test if they get the same in their system?

weekdays(as.Date("16/01/2021","%d/%m/%y"))
#[1] "Thursday"

Unless I am very confused, today it's Jan 16th 2021, and it's Saturday, not Thursday.

Worse, I was reading the story on Wikipedia about the 10 dropped days in 1582, which says:

To reinstate the association, the reform advanced the date by 10 days: Thursday 4 October 1582 was followed by Friday 15 October 1582.

This should imply that, in all countries that adopted the Gregorian calendar, the two above mentioned dates should be Thursday and Friday, respectively, and no days between them should be valid.

Well:

weekdays(as.Date("04/10/1582","%d/%m/%y"))
[1] "Sunday"
weekdays(as.Date("09/10/1582","%d/%m/%y"))
[1] "Friday"
weekdays(as.Date("15/10/1582","%d/%m/%y"))
[1] "Thursday"

So today is off by 2 days, whereas Oct 15th 1582 is off by 1 day, so it's not even linear.

I am quite puzzled by this.
Does anybody get the same output as I do, or know what may be wrong?


Solution

  • %y should be %Y, i.e. capitalized. (With %y it would only be using the first two digits of the year and so take it as 2020 and January 16, 2020 is a Thursday.)

    weekdays(as.Date("16/01/2021","%d/%m/%Y"))
    ## [1] "Saturday"