r - Get year corresponding to week number when week starts in prior year - Stack Overflow

admin2025-04-27  2

I'm trying to format dates (e.g. 2024-12-30) as year and weeknumber (e.g. 2025-01).

I have tried strftime(... , "%Y-%V"), but that gives me the wrong year for some weeks, e.g.:

strftime("2024-12-30", "%Y-%V")
[1] "2024-01"  # should be 2025-01, as December 30 is in the first week of 2025

How do I get the correct year for a weeknumber from a date?

I'm trying to format dates (e.g. 2024-12-30) as year and weeknumber (e.g. 2025-01).

I have tried strftime(... , "%Y-%V"), but that gives me the wrong year for some weeks, e.g.:

strftime("2024-12-30", "%Y-%V")
[1] "2024-01"  # should be 2025-01, as December 30 is in the first week of 2025

How do I get the correct year for a weeknumber from a date?

Share Improve this question edited Jan 11 at 14:18 jpsmith 18k6 gold badges23 silver badges45 bronze badges asked Jan 11 at 13:55 BenBen 77514 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 4

Looking at the documentation for strftime, the answer is:

strftime("2024-12-30", "%G-%V")
[1] "2025-01"
转载请注明原文地址:http://anycun.com/QandA/1745709080a91130.html