0

So given a datetime object, I want to get week of year from it with CalendarWeekRule is FirstDay and DayOfWeek is Monday.

In C# i can achieve this with:

int week = DateTimeFormatInfo.InvariantInfo.Calendar.GetWeekOfYear(DateTime, CalendarWeekRule.FirstDay, DayOfWeek.Monday);

And in python I have tried to achieve it with:

myDate.isocalendar()[1]

And

myDate.strftime("%V")

But I am unable to get correct week no for some dates like for:

1) 2015-01-01T00:00:00.000+0000

  • C# UsageWeek: 01
  • Python UsageWeek: 01

So it is right.

but for:

2) 2016-01-01T00:00:00.000+0000

  • C# UsageWeek: 01
  • Python UsageWeek: 53

This is wrong.

pault
  • 41,343
  • 15
  • 107
  • 149
Bilal Shafqat
  • 689
  • 2
  • 14
  • 26
  • @Barmar. People use to declare questions as duplicate even if previous question do not answer the the question or requirements. – Bilal Shafqat Sep 13 '19 at 09:50
  • There are multiple ways to define when the first week of the year starts, and different date libraries choose different methods. – Barmar Sep 13 '19 at 09:53
  • @Barmar. Thanks for replying, I have used these: myDate.isocalendar()[1] myDate.strftime("%V") myDate.strftime("%U") myDate.strftime("%W") But I was not able to get what I want... I want to achieve what C# is giving. – Bilal Shafqat Sep 13 '19 at 10:07
  • It looks like Python doesn't have a method that uses the `FirstDay` rule. It just has a choice between Sunday and Monday as the beginning of the week. – Barmar Sep 13 '19 at 10:12
  • Here's an answer that shows how to do it where week 1 is always Jan 1-7: https://stackoverflow.com/a/2608868/1491895. It's closer to what you want. – Barmar Sep 13 '19 at 10:14
  • @Barmar, have already gone through it. Not gives correct answer that is given by C#. – Bilal Shafqat Sep 13 '19 at 10:19
  • I know, that's why I said it's closer to what you want, not exactly the same. – Barmar Sep 13 '19 at 10:21
  • Isn't it lucky that Python allows you to write your own functions, you're not limited to built-in functions? – Barmar Sep 13 '19 at 10:22
  • yup, lucky with no luck till now. :) – Bilal Shafqat Sep 13 '19 at 10:27
  • Get the ISO week number, and get the day of week of Jan 1 that year. If the DOW is Fri-Sun, subtract 1 from the week number to get the week number you want. – Barmar Sep 13 '19 at 10:44
  • 1
    Why is this tagged pyspark? Pyspark [`weekofyear`](http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.functions.weekofyear) will return the week of the [year based on the ISO 8601 definition](https://stackoverflow.com/questions/49904570/weekofyear-returning-seemingly-incorrect-results-for-january-1). – pault Sep 13 '19 at 14:14
  • @pault. So I have a timestampType column in pyspark dataframe, I used this built-in spark function "weekofyear" to get week but it does not return what i want. I want to replicate C# functionality where CalendarWeekRule is "FirstDay" and DayOfWeek is "Monday". So I thought of achieving it through custom functionality by writing UDF. But currently now no luck. – Bilal Shafqat Sep 15 '19 at 14:59

0 Answers0