简体   繁体   English

如何从日期获取 ISO 周数

[英]How to get ISO week numbers from date

I would like to get the ISO week number from a given date in string format, where Monday is the first day of the week, and the week that contains the first Thursday of the year is considered to be the first week.我想以字符串格式从给定日期获取 ISO 周数,其中星期一是一周的第一天,包含一年中第一个星期四的那一周被视为第一周。

From other answers, I think strftime("2017-11-18", format = "%V") suits my purpose the best.从其他答案来看,我认为strftime("2017-11-18", format = "%V")最适合我的目的。 However it doesn't work on Windows.但是它在 Windows 上不起作用。

Any suggestions for alternatives with only the base package in R?对只有 R 中的基本包的替代方案有什么建议吗?

Use the package lubridate使用包lubridate

it has a function isoweek() which gives you the ISOWeek of a given date它有一个函数isoweek() ,它为您提供给定日期的 ISOWeek

lubridate::isoweek("2017-11-18")
[1] 46

Now you just want to use the base packege.现在您只想使用基本包。 Here ist the code from lubridate for the ISO week这是 ISO 周 lubridate 的代码

function (x) 
{
  xday <- make_datetime(year(x), month(x), day(x))
  dn <- 1 + (wday(x) + 5)%%7
  nth <- xday + ddays(4 - dn)
  jan1 <- make_datetime(year(nth), 1, 1)
  1L + as.integer(difftime(nth, jan1, units = "days"))%/%7L
}

we can take that an make it to something which only uses the base package.我们可以把它做成只使用基本包的东西。

myIsoweek <- function (x) 
{
  dateList <- as.list(strsplit(as.character(as.Date(x)),split = "-")[[1]])
  names(dateList) <- c("year","month","day")

  weekday <- as.POSIXlt(x)[["wday"]] + 1

  xday <- ISOdate(dateList$year, dateList$month, dateList$day)
  dn <- 1 + (weekday + 5)%%7
  nth <- xday + 86400*(4 - dn)
  jan1 <- ISOdate(format(nth,format = "%Y"), 1, 1)
  1L + as.integer(difftime(nth, jan1, units = "days"))%/%7L
}

data.table has an implementation of isoweek which you might want to just port (it's easy to replicate with base functionality) data.table拥有的实现isoweek ,你可能只想端口(它很容易复制与base功能)

# data.table approach:
isoweek <- function(x) {
  x = as.IDate(x)   # number of days since 1 Jan 1970 (a Thurs)
  nearest_thurs = as.IDate(7L * (as.integer(x + 3L) %/% 7L))
  year_start <- as.IDate(format(nearest_thurs, '%Y-01-01'))
  1L + (nearest_thurs - year_start) %/% 7L
}

Ported to be strictly base :移植到严格的base

isoweek <- function(x) {
  x = as.Date(x)   # number of days since 1 Jan 1970 (a Thurs)
  nearest_thurs = as.Date(7L * (as.integer(x + 3L) %/% 7L), origin = '1970-01-01')
  year_start <- as.Date(format(nearest_thurs, '%Y-01-01'))
  1L + (nearest_thurs - year_start) %/% 7L
}

The Python datetime library has a native function for getting ISO year, week number and week day: https://docs.python.org/3/library/datetime.html#datetime.date.isocalendar . Python datetime 库具有用于获取 ISO 年份、周数和星期几的本机函数: https : //docs.python.org/3/library/datetime.html#datetime.date.isocalendar It returns a tuple with all three values, so just select the second element if you want to have the week number only.它返回一个包含所有三个值的元组,因此如果您只想拥有周数,只需选择第二个元素。

Example:例子:

from datetime import datetime

// Create a datetime object from your string
my_date = datetime.strptime('2017-11-18', '%Y-%m-%d')
// Get the tuple of ISO date values from your datetime object
my_iso_tuple = my_date.isocalendar()
// Get the ISO week number
my_iso_week_number = my_iso_tuple[1]

我相信strftime("2017-11-18", format = "%W")应该在 Windows 中工作。

这应该工作

format(as.Date("2017-02-015"),"%W")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM