gpt4 book ai didi

c# - 如何在 python 中获取yearWeek,就像我们在 C# 中使用 DateTimeFormatInfo.InvariantInfo.Calendar.GetWeekOfYear() 一样?

转载 作者:行者123 更新时间:2023-12-01 08:09:54 28 4
gpt4 key购买 nike

我想从日期时间获取Python中的yearWeek。我已经使用了 IsoCalender()[1],但它返回的星期与我使用 DateTimeFormatInfo.InvariantInfo.Calendar.GetWeekOfYear(Date, CalendarWeekRule.FirstDay, DayOfWeek.Monday) 在 C# 中得到的星期不同。

I have tried:

1) IsoCalender()[1].

2) d = datetime.datetime.strptime('2017-09-22 00:00:00.00','%Y-%m-%d %H:%M:%S.%f')
print(datetime.datetime.strftime(d,'%W'))

So For '2017-09-22 00:00:00.00' In C# I get 39 but I get 38 in python using above mentioned techniques.

我们将非常感谢任何帮助。

最佳答案

根据 ISO 8601(参见 this draft von 2016, section 5.7.7 )

A week is defined as a seven-day time interval, starting with a Monday. Week number one of the calendar year is the first week that contains at least four (4) days in that calendar year.

Python 的文档 date.isocalendar同样指出:

[…] week starts on a Monday and ends on a Sunday. The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday.

根据documentation of CalendarWeekRule这等于CalendarWeekRule.FirstFourDayWeek:

Indicates that the first week of the year is the first week with four or more days before the designated first day of the week.

CalendarWeekRule.FirstDay 的含义是:

Indicates that the first week of the year starts on the first day of the year and ends before the following designated first day of the week.

综上所述,C#中正确的做法是:

DateTimeFormatInfo.InvariantInfo.Calendar.GetWeekOfYear(new DateTime(2017, 9, 22), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)

正确结果是 38。

关于c# - 如何在 python 中获取yearWeek,就像我们在 C# 中使用 DateTimeFormatInfo.InvariantInfo.Calendar.GetWeekOfYear() 一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55323253/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com